762

I'd like to run / open Visual Studio Code from the Mac OSX Terminal by running this command code .. I found instructions here:

https://code.visualstudio.com/Docs/setup

Apparently I need to include this in my .bashrc file, so I did, but to no avail.

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

I edited the .bashrc file here:

~/.bashrc which points to /Users/username/.bashrc

Which .bashrc should I be editing?

Johnny Oshika
  • 54,741
  • 40
  • 181
  • 275
  • 3
    have you sourced the .bashrc after adding the function? `$ source ~/.bashrc`. I would suggest you to source the `.bashrc` from `~/.bash_profile`. – Sarbbottam May 06 '15 at 00:03
  • 2
    In `OS X` you would generally add that to your `~/.bash_profile` — not `~/.bashrc` then restart `Terminal.app` or source it like mentioned. – l'L'l May 06 '15 at 00:12
  • 2
    l'L'l is right, adding the snippet to .bash_profile works – Charlie Wu May 06 '15 at 05:42
  • You were all right. It was bash_profile. Thank you! – Johnny Oshika May 06 '15 at 20:02
  • Has anyone else had the issue that this works for the "first" thing you open - but once Visual Studio Code is launched, you can't open "other" files from terminal using this technique ? – scolestock Jun 02 '15 at 14:13
  • 2
    With VS Code 0.3.0 we recommend to use a different syntax for the code command. This new syntax supports multiple arguments and correctly identifies the current working directory: code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* } – Benjamin Pasero Jun 03 '15 at 18:41
  • 2
    [This](http://stackoverflow.com/a/36882426/1374488) should be the correct answer. – lukas_o Apr 06 '17 at 09:05
  • pple in 2017 pls see @kylebrandt's comment to the answer below – swyx Jun 07 '17 at 18:01
  • 1
    Possible duplicate of [How to call VS Code Editor from command line](https://stackoverflow.com/questions/29963617/how-to-call-vs-code-editor-from-command-line) – rofrol Nov 13 '17 at 07:45
  • Explained precisely in couple of mins: https://www.youtube.com/watch?v=zWfNLB_CBFs – Om Sao Sep 17 '20 at 16:24

27 Answers27

2517

According to the docs on Launching from the command line:

  1. Open Visual Studio Code
  2. Open the command pallette with Command + Shift + P (or F1)
  3. Type Shell in command palette
  4. Select Shell Command: Install code in PATH from suggested list

Open VSCode via Terminal/Command Prompt

That's it.

Now open your terminal type.

$ code .

To make this change persist after restart on MacOS

Many Mac users find this is forgotten and needs to be re-applied after any restart. This may happen if MacOS has applied the quarantine attribute to VS Code, which the OS uses for the "Are you sure?" notice applied on first using apps downloaded from the internet.

To check if this attribute is applied, look for com.apple.quarantine in the list returned by this command (changing the path if that's not where you installed it):

xattr "/Applications/Visual Studio Code.app"

If that does return com.apple.quarantine, you can remove the attribute using the same command with the -d flag (alongside -r to recursively remove it from all contained files and sudo to allow the change):

sudo xattr -r -d com.apple.quarantine "/Applications/Visual Studio Code.app"

...then do Shell Command : Install code in PATH as above after the attribute has been removed, and it should persist after restart.

Credit: derflounder.wordpress.com article linked to by RicardoVallejo in this comment.


KyleMit
  • 30,350
  • 66
  • 462
  • 664
Raja Jaganathan
  • 33,099
  • 4
  • 30
  • 33
  • 15
    this works not by updating PATH with full path to reach VSCode, instead it introduces a symlink `/usr/local/bin/code@ -> /Applications/Visual Studio Code.app/Contents/Resources/app/bin/code` which coming from a linux background seems strange yet I guess this is how OSX rolls – Scott Stensland Aug 07 '17 at 17:00
  • 10
    For anyone experiencing the loss of the "code ." command when restarting, then you may have the code program as quarantine. https://derflounder.wordpress.com/2012/11/20/clearing-the-quarantine-extended-attribute-from-downloaded-applications – RicardoVallejo Jan 23 '19 at 10:56
  • @RZKY Do you have any reference? I'm still seeing those command in v1.44.2 – Raja Jaganathan Apr 21 '20 at 11:49
  • Works as expected. OS X 10.15.4 – bananaforscale Apr 27 '20 at 17:32
  • 7
    I've just ```ln -s "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code" /usr/local/bin/vscode``` – Andre Ravazzi May 05 '20 at 16:10
  • 9
    How come this doesn't persist, I have to do this after every restart – Cezar Cobuz Jun 22 '20 at 09:01
  • 2
    @CezarCobuz I've [editted in a section](https://stackoverflow.com/revisions/36882426/11) on how to avoid needing to re-do this after restart, based on [the link](https://derflounder.wordpress.com/2012/11/20/clearing-the-quarantine-extended-attribute-from-downloaded-applications) in [RicardoVallejo](https://stackoverflow.com/users/3429457/ricardovallejo)'s [comment](https://stackoverflow.com/questions/30065227/run-open-vscode-from-mac-terminal#comment95469686_36882426) above. – user56reinstatemonica8 Mar 22 '21 at 15:21
  • @ScottStensland they certainly do that so it works on any shell (Bash, zsh, ...) and don't take the risk to break your config. It also works for all users. – gagarine Jun 24 '21 at 12:53
  • I used to manually create symlink for `code` till today realise this can be done much easier. thx. – seedme Apr 05 '22 at 01:32
  • Thanks, It works for me on version mac 11.6.1 Big Sur – Zaenury Adhiim Jul 02 '22 at 11:38
  • doesn't macos use zsh by default? so shouldn't the op be adding those lines to their `~/.zshrc?`? – Eakan Gopalakrishnan Dec 25 '22 at 16:13
  • getting this even with sudo `xattr: [Errno 1] Operation not permitted: '/Applications/Visual Studio Code.app/Contents'` – ahmadalibaloch Jan 02 '23 at 12:38
73

I just want to pull out Benjamin Pasero's answer from inside his comment as it seems the best solution. It is the tip given on the Setting up Visual Studio Code page where it says ...

If you want to run VS Code from the terminal, append the following to your ~/.bash_profile file (~/.zshrc in case you use 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. [Or code test.txt to go to work on the test.txt file]

dumbledad
  • 16,305
  • 23
  • 120
  • 273
  • 3
    what I love about this method is that I can use "vscode" instead of "code" to launch vscode. – jj. Jun 10 '22 at 23:20
59

To setup path permanently for mac users;

open ~/.zshrc using the below command

vi ~/.zshrc

Add the following path

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

And source it using below command

source ~/.zshrc

Now close the terminal and reopen and run code . command should work properly.

Vaibhav Jain
  • 629
  • 5
  • 5
32

follow some simple steps :

  1. open your visual studio code (vs code).
  2. press F1.
  3. pallete will open in top center with symbol >
  4. type shell .
  5. select intall 'code' command in PATH.
  6. it will be automatically intalled.

Now you can use from terminal by typing

$ code .

sherkhan
  • 811
  • 8
  • 8
23

Open VSCode, press Command + Shift + P, type Shell in command palette, Select that option => Install code in PATH from suggested list in command palette.

Ahmed Raza
  • 401
  • 4
  • 6
22

If you are on Mac OSX Maverick, it's ~/.bash_profile not ~/.bashrc

Try putting the code in there, close the terminal and then try again. Should be working

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
Jabran Saeed
  • 5,760
  • 3
  • 21
  • 37
  • 13
    This is no longer the up to date method, use the instructions in http://stackoverflow.com/a/36882426/107156 instead. This is now a built-in feature to code, and you just have to tell it to install it for you. – Kyle Brandt May 12 '16 at 19:42
  • thanks @KyleBrandt for posting the updated instructions – swyx Jun 07 '17 at 18:01
19

For Mac you can do : View > Command Palette > Shell command > "install code command in path". I'd assume there would be something similar for other OS's. After I do

which code

and it tells me it put it in /usr/local/bin

JGFMK
  • 8,425
  • 4
  • 58
  • 92
  • 1
    This answer doesn't answer the question exactly, but this command is a good addition to my knowledge of commands. This is very helpful to find the source of any command that we use. – Aswin Prasad Jul 16 '20 at 08:48
  • 1
    My thought was ... as long as I can type `code` and it launches from the terminal that'll do for me. I used which to see where it put it. Had it not worked automatically I would have gone editing profiles etc. I like the 'kiss' rule ;-) – JGFMK Jul 16 '20 at 13:06
18

For macOS 12.0 and above:

  1. Open profile in Notepad
open ~/.zshrc
  1. Create an alias for code, Paste below:
alias code='open -a "Visual Studio Code"' # open file or folder in VSCode e.g. code ~/.zshrc
  1. Now you can open the current folder e.g. code . or any other file/folder by providing its path.

  2. Profit


PS: You can add as many aliases as needed to open a file/folder with different editors. Just mention the editor's name in the alias. For example, open file/folder with sublime text:

alias subl='open -a "Sublime Text"' # open file or folder in sublime e.g. subl ~/.zshrc

And use it like subl .

GorvGoyl
  • 42,508
  • 29
  • 229
  • 225
17

Sometimes, just adding the shell command doesn't work. We need to check whether visual studio code is available in "Applications" folder or not. That was the case for me.

The moment you download VS code, it stays in "Downloads" folder and terminal doesn't pick up from there. So, I manually moved my VS code to "Applications" folder to access from Terminal.

Step 1: Download VS code, which will give a zipped folder.

Step 2: Run it, which will give a exe kinda file in downloads folder.

Step 3: Move it to "Applications" folder manually.

Step 4: Open VS code, "Command+Shift+P" and run the shell command.

Step 5: Restart the terminal.

Step 6: Typing "Code ." on terminal should work now.

Balaji
  • 215
  • 3
  • 10
15

To set up VS code path permanently on Mac OS;

just open .bash_profile using the following command on terminal

open -t .bash_profile

Then add the following path to .bash_profile

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

save the .bash_profile file and quit the terminal. Then reopen the terminal and type code .to open VS code.

Kalhara Tennakoon
  • 1,302
  • 14
  • 20
  • Great answer. To add a tip: If you don't want to restart the terminal, after you edit the .bash_profile run `source ~/.bash_profile` and it reloads the latest configuration in the current terminal. – Bere Feb 11 '21 at 10:26
14

Somehow using Raja's approach worked for me only once, after a reboot, it seems gone. To make it persistent across Mac OS reboot, I added this line into my ~/.zshrc since I'm using zsh:

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

source ~/.zshrc now, I could just do

code .

even after I reboot my Mac.

Fisher Coder
  • 3,278
  • 12
  • 49
  • 84
13

For Mac users:

One thing that made the accepted answer not work for me is that I didn't drag the vs code package into the applications folder

So you need to drag it to the applications folder then you run the command inside vs code (shown below) as per the official document

  • Launch VS Code.
  • Open the Command Palette (⇧⌘P) and type 'shell command' to find the Shell Command: Install 'code' command in PATH command.
Ahmed Elkoussy
  • 8,162
  • 10
  • 60
  • 85
10
  1. Open Visual Studio Code
  2. Open the command pallette with Command + Shift + P
  3. Type Shell in command palette
  4. Select Shell Command: Install code in PATH from suggested list
  5. Type code . in mac terminal
Shikhar Saxena
  • 101
  • 1
  • 3
8

Yo do this:

  1. Launch Visual Studio Code.
  2. Press Cmd ⌘ + Shift ⇧ + P to open the Command Palette.
  3. Type in shell command and select the Shell command: Install ‘code’ command in PATH to install it.
7

How about a simple Bash alias that you stick in your .bash_profile ?

alias code="open -a /Applications/Visual\ Studio\ Code.app"

To open the current directory:

code .

Wolfgang
  • 1,408
  • 2
  • 15
  • 20
4

I follow this step it work for me .

first open VSC . open terminal of VSC.

Press cmd+shift+p

uninstall path . Give permission to it.

Press cmd+shift+p

install path .

then open Mac terminal navigate to root project file type

code . in root folder It will open VSC . :)

Rushikesh
  • 66
  • 4
3

I just made a symbolic link from the "code" program supplied in the Visual Studio Code.app bundle to /usr/local/bin (a place where I prefer to put stuff like that and which is already in my path on my machine).

You can make a symbolic link using ln -s like this:

ln -s /Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code /usr/local/bin/code

brant
  • 369
  • 3
  • 8
2

To set it up, launch VS Code. Then open the Command Palette (⇧⌘P) and type shell command to find the Shell Command: Install 'code' command in PATH command.enter image description here

https://code.visualstudio.com/docs/setup/mac

menzil
  • 29
  • 2
2

add below snipped in your bash profile -

PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;}
Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107
2

I moved VS Code from Downloads folder to Applications, and then i was able to run code in the terminal. I guess, it might help you too.

daniilorain
  • 121
  • 1
  • 7
2
  1. Open VSCode
  2. Go to view --> Command Palette
  3. Search for "shell...install 'code'
  4. Open your terminal and place in the directory you wanna open
  5. Use $ code .
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 22 '22 at 12:30
1

I simply created a file called code:

#!/bin/bash

open /Applications/Visual\ Studio\ Code.app $1

Make it executable:

$ chmod 755 code

Then put that in /usr/local/bin

$ sudo mv code /usr/local/bin

As long as the file sits someplace that is in your path you can open a file by just typing: code

Rich
  • 57
  • 3
1

I prefer to have symlinks in the home directory, in this case at least. Here's how I have things setup:

: cat ~/.bash_profile | grep PATH
# places ~/bin first in PATH
export PATH=~/bin:$PATH

So I symlinked to the VSCode binary like so:

ln -s /Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code ~/bin/code

Now I can issue code . in whichever directory I desire.

Daniel Lidström
  • 9,930
  • 1
  • 27
  • 35
1

Since, default shell is zsh in macOS, you can try this:

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

This will add a path to your VS Code, restart your terminal and voila, you're good to go.

code example.py

Ankit Kumar
  • 83
  • 10
1
alias code="/Applications/Visual\ Studio\ Code\ 2.app/Contents/Resources/app/bin/code $1"

the alias to the vs code's bin file with parameters works well

you can do code . after having sourced your bash file

lobamo
  • 11
  • 2
0

open finder and go to applications and make sure that vscode exists there ,then open type in terminal export PATH="/Applications/Visual Studio Code.app/Contents/Resources/app/bin"

HeshamSalama
  • 148
  • 4
-1
code () {
    if [[ $# = 0 ]]
    then
        open -a "Visual Studio Code"
    else
        echo "Opening: "$@
        "/Applications/Visual Studio Code.app/Contents/MacOS/Electron" $@
    fi
}

I put that into my .bash_profile I tested it and it works.

Scourge
  • 73
  • 5