1296

The command code . doesn't work in this manual.

All the other steps before that worked. How can I call the Visual Studio Code in an OS X terminal?

pwd

/Users/mona/nodejs/myExpressApp

code .

-bash: code: command not found


I ended up opening it from within Visual Code Studio by changing the workspace, but why won't that command code . part work?

Enter image description here

4
  • youtube.com/watch?v=YereIjEJF7s Commented Dec 2, 2021 at 13:00
  • I reinstalled homebrew from scratch in arm64/m1 environment, and /usr/local is no longer in PATH. It's /opt/homebrew now Commented Feb 15, 2022 at 20:37
  • 4
    if you have install vscode-insider version on mac m1 than you need to run code-insiderr .
    – owais
    Commented Mar 31, 2022 at 9:24
  • 4
    it should be code-insiders .
    – Rotkiv
    Commented Aug 19, 2022 at 0:42

37 Answers 37

3472

1. Make sure you drag the Visual Studio Code application into the Applications folder

Otherwise (as noted in the comments), you'll have to go through this process again after a reboot.

For example, sometimes the VSCode application might be in your downloads folder, so make sure to move it out of there.


2. Next, open Visual Studio Code

Open the Command Palette via P and type shell command to find the Shell Command:

Use the Uninstall 'code' command in the PATH command before the "Install 'code' command in PATH" command.

![Command Palette

After executing the command, restart the terminal for the new $PATH value to take effect. You'll be able to simply type 'code .' in any folder to start editing files in that folder. The "." Simply means "current directory"

(Source: Visual Studio Code documentation)


👉 Remember to Restart your machine afterwards

Note: If you're running a build based off the OSS repository, you will need to run code-oss . (Dzeimsas Zvirblis' comment)


EACCES: permission denied, unlink '/usr/local/bin/code'

If you see this EACCES error, just run the "Uninstall 'code' command from PATH, then the 'install' it again, and you'll be all set!

26
  • 112
    this does work, but after reboot I have to do it again.
    – uloco
    Commented May 22, 2017 at 16:37
  • 5
    It looks like the Shell Command: Install 'code' command in PATH in VSCode just creates a symlink in /usr/local/bin/ now
    – jlucktay
    Commented Jun 6, 2018 at 8:28
  • 66
    Make sure you drag Visual Studio Code.app into the Applications folder. Otherwise, as @uloco said, you'll have to go through this process again after reboot. Commented Jul 31, 2018 at 0:44
  • 32
    even after dragging vscode into Applications, I've to do it every time I restart.
    – x86-64
    Commented Mar 20, 2020 at 0:35
  • 10
    Even in late 2020 I had this problem, and the suggestion of moving to Applications folder fixed it for me. The other thing I had to do was to get rid of the VSCode shortcut I had on on the dock b/c it was pointing to where I previously downloaded (~/Downloads/).
    – shaune
    Commented Dec 22, 2020 at 21:41
332

If you want to add it permanently:

Add this to your ~/.bash_profile, or to ~/.zshrc if you are running macOS v10.15 (Catalina) or later. (To add, cd ~ and vim ~/.bash_profile in terminal)

export PATH="$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"

This assumes that you have VS Code in /Applications/Visual Studio Code.app; if you have it installed in a different location ... probably move it to Applications (or modify the code to point to your custom location).

Elaborating the process to add to the file:

  1. Open terminal and go to base directory using cd ~
  2. Then, vim ~/.bash_profile inorder to edit the file
  3. Press i for making an insertion and paste the string export PATH="$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin" at the end
  4. Save and close using :wq
  5. Once this is done, paste source ~/.zshrc

Source: Visual Studio Code on macOS

9
  • 5
    Look like now code is under /usr/local/bin, so it might be better to update the answer :) Commented May 18, 2020 at 5:35
  • 7
    worked for me, please specify 2 more points here, you need to write the PATH variable by vim ~/.zshrc and run source ~/.zshrc to reload zsh
    – codebusta
    Commented Mar 18, 2021 at 8:47
  • Worked well for me with ~/.zshrc on Catalina 10.15.7
    – Tom Hert
    Commented Apr 14, 2021 at 6:31
  • This worked for me despite the fact that using the VS Code Install 'code' command in PATH command results in which code returning /usr/local/bin`. That was already on my PATH the true location of the executable is where you've installed VS COde. Commented Jul 1, 2021 at 12:49
  • 9
    Note this only works if Visual Studio Code is actually installed in the Applications folder. Mine was sitting in Downloads!
    – Gamma032
    Commented Jan 28, 2022 at 4:14
133

Go the top of VS and select menu ViewCommand Palette...

  • [macOS] Open the Command Palette via P and type shell command to find the Shell Command:
  • [Windows] Open the Command Palette via Ctrl+Shift+P and type shell command to find the Shell Command:

Type: shell command

enter image description here And install.

3
  • This is nearly identical to Josie Koay's answer. Commented Apr 1, 2022 at 20:34
  • 1
    Is "shell command" literal or not? It doesn't seem to do anything—"No matching commands" (version 1.66 of Visual Studio Code. On Linux, though). Is it dependent on some extension being installed? What version of Visual Studio Code was it tried on? Commented Apr 1, 2022 at 20:37
  • If someone gets the error as "EACCES: permission denied", then please follow this answer: stackoverflow.com/a/69219632/885627 Uninstall the code command from PATH in VS Code and reinstall it. Open the command palette in VS Code using cmd + shift + p. Search "uninstall 'code'" and select the first option to uninstall. Once uninstalled, "install 'code'" for installing. That should fix it. Commented Jul 16, 2023 at 10:03
77

Open the ~/.bashrc file using vi or Vim:

vi ~/.bashrc

Enter the following by pressing i to insert:

code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;}

Save and exit the file using Esc + :wq

Reflect the settings in ~/.bashrc using the following command:

source ~/.bashrc
4
  • this worked for me, I was getting an error "cannot execute binary file" after my path probably got messed somehow - but now it won't open any files if i do code filename, just opens an empty window of VS code
    – Ali
    Commented May 15, 2020 at 12:16
  • 1
    Does this still work? The default shell changed to Z shell in macOS v10.15 (Catalina). Commented Apr 29, 2022 at 19:58
  • The file you need to modify is different if you are using Zsh. You'll want to put this into your .zprofile instead, and then of course source that file instead (or just paste the same snippet at your terminal prompt; there is no specific requirement to load it from your .zprofile and there are situations where you want to avoid sourcing it a second time).
    – tripleee
    Commented Jan 8 at 6:23
  • Strictly speaking you want "$@", not $*. The latter will work incorrectly when you try to edit files whose names contain spaces, etc.
    – tripleee
    Commented Jan 8 at 6:45
59

Use:

sudo rm /usr/local/bin/code

Open Visual Studio Code, and then press Ctrl + Shift + P

Enter image description here

And write command install code and you will get a popup. Then follow the instruction and done!

4
  • this is just temporary solution, because after reboot need to do it again.
    – MJ Montes
    Commented Nov 16, 2021 at 10:51
  • does not work for mac os monterey
    – james-see
    Commented Mar 2, 2022 at 4:19
  • 1
    Okay, let me update a permanent solution for any mac os version.
    – Amit Kumar
    Commented Mar 2, 2022 at 7:37
  • This seems like a solution for a very specific scenario which will be exactly wrong in many others.
    – tripleee
    Commented Oct 11, 2023 at 11:55
50

For those of you that run Z shell with iTerm2, add this to your ~/.zshrc file:

alias code="/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code"
6
  • Thanks, zsh user here too, this was just what I needed, though my path was a little different in order to get it to work: alias code="/Applications/VisualStudioCode.app/Contents/Resources/app/bin/code"
    – AM_FM
    Commented Sep 12, 2019 at 16:04
  • It didn't work for me. Now it doesn't even recognise code anymore
    – AMMA
    Commented Sep 28, 2020 at 19:09
  • @AMMA I would think it has a lot to do with where you installed your VSCode. Commented Sep 29, 2020 at 19:11
  • 3
    Z shell became the default shell with macOS v10.15 (Catalina)—released about half a year after this answer. Commented Apr 1, 2022 at 17:55
  • Been very irritated having to reinstall it every time I restart my macbook THIS answer fixed it! Incase it doesn't work for you you can also try to change the path to /usr/local/bin/code Commented Sep 21, 2022 at 7:52
47

Steps to follow:

  1. Open the Visual Studio Code application, and type Command + Shift + P and type the command 'install code'. Then enter it.
  2. You will see the below message in Visual Studio Application: shell command ' code' successfully installed in PATH.
  3. Now, jump to the iTerm CLI and type code .
  4. Then you will be able to redirect to Visual Studio Code for any code change/view.
6
  • 2
    this worked for me. thanks! For mac users, make sure vs code is in the applications folder.
    – tngeene
    Commented Apr 20, 2021 at 6:49
  • Show us instead of telling us. I'm a new user and I don't even now where the applications folder is, and no idea of to "make sure". Commented Jun 20, 2021 at 12:34
  • 2
    No matching commands for me.
    – Drew
    Commented Nov 9, 2021 at 5:17
  • @NiklasRosencrantz A picture is much more ambiguous than an explanation. Your applications folder is named /Applications (though if you have a localized MacOS it might be displayed in your own language. Even then, this path should work e.g. in Finder).
    – tripleee
    Commented Jan 8 at 6:39
  • @tripleee You are wrong. An explanation won't convince everybody. An engineer can't sit in a meeting and explain why his machine works or doesn't. It won't get through. If you have landed on Mars and completed the mission, a picture will be a lot more convincing than a guy sitting in a meeting and telling stories. Don't believe me. Try it yourself. Sit in a meeting and try to explain why hardware or software works or doesn't work, especially to a non-expert. Show a picture of a car-crash it it immediately communicates everything. Commented Feb 11 at 6:20
45

On my Mac I got it working:

Add to file .bash_profile:

code() {
   open -a Visual\ Studio\ Code.app $1
}

Save and in the terminal do source .bash_profile.

Then in the terminal, code index.html (or whatever) will open that file in Visual Studio Code.

2
  • This also worked in .zprofile
    – Rpant
    Commented May 2, 2023 at 14:46
  • 1
    This breaks when $1 contains spaces or other shell metacharacters. The minimal fix is to use "$1" instead; but you probably want "$@" which allows you to specify more than one argument. Also, perhaps mention that this requires Visual Studio Code.app to be in your /Applications folder.
    – tripleee
    Commented Jan 8 at 6:29
30

Setup code. In just one second

Just follow one simple command with the following steps:

  1. Open the Visual Studio Code application

  2. Command + Shift + P

  3. Type command 'install code'. Then enter it.

Boom, it’s done.

Now use the command Code . where you want to use it.

2
  • 1
    I don't think "Code ." will work for the question here (Mac). It may work on a Windows system. On a Linux system I get "Command 'Code' not found". Commented Apr 1, 2022 at 19:58
  • 1
    @PeterMortensen it's code ., the OP capitalized it incorrectly
    – Talal916
    Commented Apr 28 at 10:19
29

Here are the steps which I followed to make it work on Mac:

Install the "Shell" extension from Visual Studio Code:

Enter image description here

Restart Visual Studio Code.

Press F1 when Visual Studio Code is opened.

Type "Shell" and select the following option: Shell Command: Install 'code' command in PATH command:

Enter image description here

That will give you the following message: Shell command 'code' successfully installed in PATH.

Enter image description here

Running the "which code" command will give you a proof the 'code' command is working now:

Enter image description here

2
28

For Mac OS X, there are three ways you can enable code . to open the current folder in Visual Studio Code.

For a fresh installation

Install Visual Studio Code through Homebrew

There is a way to install Visual Studio Code through Brew-Cask.

  1. First, install Homebrew from here.

  2. Now run the following command, and it will install the latest Visual Studio Code on your Mac.

    brew cask install visual-studio-code
    

The above command should install Visual Studio Code and also set up the command-line calling of Visual Studio Code.

If the above steps don't work then you can do it manually. By following Microsoft Visual Studio Code documentation given here.

If Visual Studio Code is already installed

If Visual Studio Code is already installed then you don't have to reinstall it. You can follow any of the below two options.

Option 1: Update PATH in the Bash profile

Update your favorite Bash profile, such as ~/.bash_profile or ~/.bashrc or ~/.zshrc by exporting the app/bin path of the Visual Studio Code application. You can add the below export command to your favorite Bash profile.

export PATH="$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"

Option 2: Using the Visual Studio Code Command Palette

We can run a shell command in the Visual Studio Code Command Palette too. To do so, we need to open the Command Palette via (⇧⌘P) and type "shell command" to find the shell command named as:

Shell Command: Install 'code' command in PATH

Press Enter to execute the above shell command.

Enter image description here

That's it.

0
27

Please follow below steps

  1. Open VSCode
  2. Press command + p.
  3. Type >shell command in the input box at the top.
  4. A dropdown will come, now select install code command in PATH option.

enter image description here

2
  • I do not have to reload the vscode application in order to use the code command. This fully works for me.
    – Franco Gil
    Commented Nov 3, 2023 at 15:45
  • Thank you for the solution. This works for me. However, I need to do it every day to use the code command in the terminal. Please let me know if there is any solution for the same. Commented Dec 14, 2023 at 10:01
16

I tried this by following the documentation and it works for me:

  1. Launch Visual Studio Code

  2. Open the Command Palette (Cmd + Shift + P) and type 'shell command' to find

    Shell Command: Install 'code' command in PATH command

    Enter image description here

  3. Restart terminal

1
15

This works for me:

sudo ln -fs "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code" /usr/local/bin/
4
  • Why does it work? What is the gist of it? An explanation would be in order. From the Help Center: "...always explain why the solution you're presenting is appropriate and how it works". Please respond by editing (changing) your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today). Commented Apr 1, 2022 at 18:14
  • 1
    I can't speak for OP, but a version of this worked for me because a ls -lah /usr/local/bin showed that code was in there and linked to a long directory path...that didn't exist. So I just deleted it and made a new link to the /Application folder path that did exist. Now when the shell looks for code in /usr/local/bin, it find it, and not a broken link.
    – Daniel
    Commented Apr 16, 2022 at 18:04
  • If you use windows, you can use sudo ln -s "/mnt/c/Users/xxx/AppData/Local/Programs/Microsoft VS Code/bin/code" /us r/bin/code. * xxx means PC User
    – Jack
    Commented Oct 18, 2023 at 0:26
  • Note that -f tells ln to unceremoniously replace any existing link without notifying you. Perhaps look before you jump.
    – tripleee
    Commented Jan 8 at 6:41
15

See Setting up Visual Studio Code

Tip: If you want to run Visual Studio Code from the terminal, append the following to your .bashrc file file:

code () {
if [[ $# = 0 ]]
then
    open -a "Visual Studio Code"
else
    [[ $1 = /* ]] && F="$1" || F="$PWD/${1#./}"
    open -a "Visual Studio Code" --args "$F"
fi
}

Then source ~/.bashrc

2
  • Yes. The source ~/.bashrc is missing from the official docs.
    – Greg B
    Commented May 6, 2015 at 20:02
  • This only allows you to pass in a single argument, and it cannot be an option. I don't see that the PWD mogrification would be necessary or useful; open -a already knows which directory you are in.
    – tripleee
    Commented Jan 8 at 6:54
12

It was quite simple to follow the documentation to install 'code' to PATH, but it didn't work.

I simply uninstalled it first and then installed it again.

Open the Command Palette (⇧⌘P)

Shell Command: Uninstall 'code' command in PATH command.

Then install it again.

Shell Command: Install 'code' command in PATH command.

Restart your terminal to have the new PATH included.

0
8

VSCodium

If you came here from search engine and are a free/open source user like me looking for VSCodium (Free/Libre Open Source of VSCode) instead of Microsoft spyware VSCode, then here is a little change for you.

In VSCodium binary executable code is called codium.

So, when you open the Command Line Terminal via ++P and type in shell command to find the codium.

You should be able to open VSCodium using:

codium .

7

If you are using Visual Studio Code Insiders build:

code-insiders .

With regular Visual Studio Code:

code .
0
6

For that to work, there needs to be an executable named 'code' in your Bash path, which some installers add for you, but this one apparently did not.

The best way to do this could be to add a symbolic link to the Visual Studio Code application in your /usr/local/bin folder. You can do this by using a command like the following in your terminal.

ln -s "/Path/To/Visual Studio Code" "/usr/local/bin/code"

You will likely need to put sudo in front of that to have the permissions for it to complete successfully.

3
  • 5
    "/Applications/Visual\ Studio\ Code.app/Contents/MacOS/Electron" should be the target Commented Nov 24, 2015 at 9:04
  • If you use windows, you can use sudo ln -s "/mnt/c/Users/xxx/AppData/Local/Programs/Microsoft VS Code/bin/code" /us r/bin/code. * xxx means PC User
    – Jack
    Commented Oct 18, 2023 at 0:25
  • The above comment may be obsolete. Electron runs some parts, but as of 2024, the real thing is in /Applications/Visual Studio Code.app/Contents/Resources/app/bin/code (and also, you want either double quotes around the value, or escape the spaces with a backslash; but not both).
    – tripleee
    Commented Jan 8 at 6:34
4

I foolishly deleted my /usr/local/bin/code symbolic link and did not know the correct path. A Homebrew reinstall recreated it:

brew cask reinstall visual-studio-code

The path turned out to be:

/usr/local/bin/code ->
'/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code'
1
  • Might be worth updating this answer for newer versions of brew, otherwise you'll get this error: Error: brew cask` is no longer a brew command. Use brew <command> --cask instead.`
    – Raleigh L.
    Commented Aug 5, 2023 at 21:52
3

Mac OS X

  1. Download Visual Studio Code for Mac OS X.

  2. Double-click onVSCode-osx.zip to expand the contents.

  3. Drag Visual Studio Code.app to the Applications folder, making it available in the Launchpad.

  4. Add Visual Studio Code to your Dock by right-clicking on the icon and choosing Options, Keep in Dock.

Tip: If you want to run Visual Studio Code from the terminal, append the following to your ~/.bash_profile file (~/.zshrc in case you use Z shell (executable zsh)).

code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;}

Now, you can simply type code . in any folder to start editing files in that folder.

1
  • $* isn't really correct; you want "$@". This seems like roughly a duplicate of Mick dK's answer from 2015.
    – tripleee
    Commented Jan 8 at 6:37
3

Make sure VS Code is installed and moved to Application section

Open VS Code

Open the Command Palette (⇧⌘P)

Shell Command: Uninstall 'code' command in PATH command.

Then install it again.

Shell Command: Install 'code' command in PATH command.

Restart Terminal (Better OS)

1
2

If this is happening on Linux Mint or Ubuntu, it is likely because you installed Visual Studio Code through the software manager. This will cause other problems during debugging. Instead install it using the .deb file on the Visual Studio Code website.

If you really want to use the software manager, the solution below still works:

Use find / -name code 2> /dev/null to find the path to the Visual Studio Code binary file. It should end in /extra/vscode/bin/code

If you're using the Linux Mint software manager, you might only find paths with a ridiculously long name in the middle like this:

".../stable/7a22830d9e8fbbdc9627e43e072005eef66c14d2a4dd19992427ef4de060186a/..."

Just replace the long part with "/active/"

Once you have it, create a symbolic link:

ln -s path_you_found/extra/vscode/bin/code /usr/local/bin/code

If you don't have the rights, or only want it to be accessible for yourself, simply add this line to your .bashrc or .zshrc file:

export PATH="$PATH:path_you_found/extra/vscode/bin/

Note that I removed the 'code' filename at the end.

1
  • Yes, but the question was about OS X/macOS. Commented Apr 1, 2022 at 17:52
2

If you are on Windows and facing the same problem, have a look at Inazense's answer, Visual Studio Code - "Shell Command: Install 'code' command in PATH command.".

In Visual Studio Code I was not able to find “Shell Command: Install 'code' command in PATH command.” so I had to do this manually.

  1. Open Environment Variables (SystemAdvanced system settingsAdvanced tab → Environment variables). In system variables, click on Path and click Edit and add a new path named:

"C:\Users\Your_Username\AppData\Local\Programs\Microsoft VS Code\bin"

Now you are done! Restart the command prompt and try again.

1
  • Yes, but the question is about OS X/macOS. Commented Apr 1, 2022 at 18:09
1

I was having the same problem. I had to add Visual Studio Code to my applications folder. It worked without editing a file.

  1. Open the Applications folder

    Enter image description here

  2. Search for Visual Studio Code in your search

    Enter image description here

  3. Drag Visual Studio Code to the Applications folder

    Enter image description here

This will work for you.

1

If you have trouble using the Command Palette solution, you can manually add Visual Studio Code to the $PATH environment variable when your terminal starts:

cat << EOF >> ~/.bash_profile
# Add Visual Studio Code (code)
export PATH="$PATH:/Applications/Visual Studio
Code.app/Contents/Resources/app/bin"
EOF
1

Alternative to a command line solution:

Recently I was playing with services in Mac OS X. I added a service to a folder or file so that I can open that folder or file in Visual Studio Code. I think this could be an alternative to using the 'code .' command if you are using the Finder app.

Here are the steps:

  • Open Automator App from Application (or you can use Spotlight).

  • Click on the New Document button to create a new script.

  • Choose 'Service' as a new type of document.

  • Select 'files and folders' in 'Service receives selected' dropdown.

  • Search for 'Open Finder Items' action item.

  • Drag that action item to the workflow area.

  • Select the 'Visual Studio Code.app' application in the action 'Open with' dropdown.

  • Press Command + S to save the service. It will ask a name of service. Give it a name. I gave 'Open with Visual Studio Code'. Close the Automator app. Check the image below for more information.

    Enter image description here

Verify:

  • Open the Finder app.

  • Right-click on any folder.

  • In the context menu, look for 'Open with Visual Studio Code' menu option.

  • Click on the 'Open with Visual Studio Code' menu option.

  • The folder should get open in the Visual Studio Code application. Check image below for more information.

    Enter image description here

0

Define the path of the Visual Studio Code in your ~/.bash_profile file as follows:

export PATH="$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"
0

Note: with Code Insiders for Visual Studio Code 1.58 (June 2021), you have Microsoft/Visual Studio Code issue 126702 (on Windows, but also Mac).

code-insiders . is not opening the current directory. It opens the path to code insiders instead.

This has been fixed.

0

For macOS, search for the Visual Studio Code application. For example, it was in my Downloads section.

Now copy that to the Applications folder and then run the following commands.

  • Open terminal and type vi ~/.zshrc
  • Add this line at the end (if not empty) export PATH="$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"
  • Press Command + Q (in short, quit the terminal)
  • Again open up the terminal and go to the Git code folders

You are all sorted now.

2
  • What is the "Downloads section"? The web browser's download folder? Or something Mac-specific? Commented Apr 1, 2022 at 20:07
  • @PeterMortensen Presumably they downloaded the binary in their browser and so it was in their ~/Downloads folder (not "section").
    – tripleee
    Commented Jan 8 at 6:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.