Apple - How to install Visual Studio Code's `code` command permanently?

Looks like your whole Visual Studio got installed in a temporary folder.

  • Remove the current link with sudo rm /usr/local/bin/code

  • Start Visual Studio from within /Applications

  • Reinstall code as described in https://stackoverflow.com/a/29971430:

    Now open the Command Palette (F1 or ⇧⌘P on Mac) and type shell command to find the Shell Command: Install 'code' command in PATH command.

If it still doesn't work you can also link manually by running

sudo ln -fs "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code" /usr/local/bin/

If you're on Mac OS Catalina, you need to edit your .zprofile instead of .bash_profile.

  1. Edit your ~/.zprofile file: vim ~/.zprofile
  2. Add the following code in it, on it's own line: code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;}
  3. Save the file: :wq
  4. Re-run the updated file: source ~/.zprofile.
  5. Test that running code . opens your current folder in VS Code!

OK, it seems "ln" command not work for me (I use this command set up sublime text). But I got an another solution.

  1. edit your ~/.bash_profile file.

  2. add the following code in it. code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;}

  3. after save. do not forget to run source ~/.bash_profile.

  4. Finished and test it, type code

Tags:

Macos