[macos] Run / Open VSCode from Mac Terminal

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?

This question is related to macos terminal visual-studio-code

The answer is


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


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.


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


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


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.


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.

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.


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]


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


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"


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.


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 $* ;}

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.


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


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 .


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.


Examples related to macos

Problems with installation of Google App Engine SDK for php in OS X dyld: Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac Could not install packages due to an EnvironmentError: [Errno 13] How do I install Java on Mac OSX allowing version switching? Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Can't compile C program on a Mac after upgrade to Mojave You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory. (mac user) How can I install a previous version of Python 3 in macOS using homebrew? Could not install packages due to a "Environment error :[error 13]: permission denied : 'usr/local/bin/f2py'"

Examples related to terminal

Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Can't compile C program on a Mac after upgrade to Mojave Flutter command not found VSCode Change Default Terminal How to switch Python versions in Terminal? How to open the terminal in Atom? Color theme for VS Code integrated terminal How to edit a text file in my terminal How to open google chrome from terminal? Switch between python 2.7 and python 3.5 on Mac OS X

Examples related to visual-studio-code

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined raised when starting react app Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error Cannot edit in read-only editor VS Code How to setup virtual environment for Python in VS Code? Pylint "unresolved import" error in Visual Studio Code Why do I keep getting Delete 'cr' [prettier/prettier]? How to set up devices for VS Code for a Flutter emulator VSCode single to double quote automatic replace js 'types' can only be used in a .ts file - Visual Studio Code using @ts-check How can I clear the terminal in Visual Studio Code?