Run / Open VSCode from Mac Terminal

354,541

Solution 1

Try this one

Open Visual Studio Code and press Command + Shift + P or F1 then type Shell in command palette now you are able to find this option like Shell Command : Install code in PATH from suggested list in command palette. Select that options.

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.


Solution 2

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]

Solution 3

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.

Solution 4

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 .

Solution 5

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

Share:
354,541
Johnny Oshika
Author by

Johnny Oshika

I’m a serial entrepreneur and a passionate software architect! I co-founded 3 companies (BC Jobs, Jobcast, PropertyInsight) and got lucky and sold 2 of them. I’m always learning and upgrading my skills, so if you're innovating on a cool new project, I'd love to hear from you! Message me on LinkedIn (https://www.linkedin.com/in/johnnyoshika/) or find me on GitHub (https://github.com/johnnyoshika).

Updated on February 14, 2022

Comments

  • Johnny Oshika
    Johnny Oshika about 2 years

    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?

  • Kyle Brandt
    Kyle Brandt almost 8 years
    This is no longer the up to date method, use the instructions in 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.
  • Stephen Rauch
    Stephen Rauch about 7 years
    How does this differ from the highest voted answer already here?
  • swyx
    swyx almost 7 years
    thanks @KyleBrandt for posting the updated instructions
  • Scott Stensland
    Scott Stensland over 6 years
    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
  • RicardoVallejo
    RicardoVallejo over 5 years
    For anyone experiencing the loss of the "code ." command when restarting, then you may have the code program as quarantine. derflounder.wordpress.com/2012/11/20/…
  • Raja Jaganathan
    Raja Jaganathan about 4 years
    @RZKY Do you have any reference? I'm still seeing those command in v1.44.2
  • bananaforscale
    bananaforscale about 4 years
    Works as expected. OS X 10.15.4
  • Andre Ravazzi
    Andre Ravazzi almost 4 years
    I've just ln -s "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code" /usr/local/bin/vscode
  • Cezar Cobuz
    Cezar Cobuz almost 4 years
    How come this doesn't persist, I have to do this after every restart
  • Aswin Prasad
    Aswin Prasad almost 4 years
    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.
  • JGFMK
    JGFMK almost 4 years
    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 ;-)
  • Bere
    Bere about 3 years
    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.
  • Ganesh Chowdhary Sadanala
    Ganesh Chowdhary Sadanala about 3 years
    Does not work after closing and opening terminal. Works only for one session
  • Ganesh Chowdhary Sadanala
    Ganesh Chowdhary Sadanala about 3 years
    Official Documentation works properly suggested by @Raja Jaganathan
  • user56reinstatemonica8
    user56reinstatemonica8 about 3 years
    @CezarCobuz I've editted in a section on how to avoid needing to re-do this after restart, based on the link in RicardoVallejo's comment above.
  • gagarine
    gagarine almost 3 years
    @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.
  • seedme
    seedme about 2 years
    I used to manually create symlink for code till today realise this can be done much easier. thx.
  • Jesse H.
    Jesse H. almost 2 years
    I prefer this solution since I did not care to overwrite my existing $PATH variable.
  • jj.
    jj. almost 2 years
    what I love about this method is that I can use "vscode" instead of "code" to launch vscode.
  • Zaenury Adhiim
    Zaenury Adhiim almost 2 years
    Thanks, It works for me on version mac 11.6.1 Big Sur