603
votes

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 OSX terminal?

Monas-MacBook-Pro:myExpressApp mona$ pwd
/Users/mona/nodejs/myExpressApp
Monas-MacBook-Pro:myExpressApp mona$ code .
-bash: code: command not found

EDIT: I ended up opening it from within Visual Code Studio by changing the workspace but I am wondering why that command "code ." won't work? enter image description here

29

29 Answers

1688
votes

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

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


2. Next, open Visual Studio Code

Open the Command Palette via (⇧⌘P) and type shell command to find the Shell Command:

> 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: VS Code documentation)


NOTE: If you're running a build based off the OSS repository... You will need to run code-oss . @Dzeimsas Zvirblis

132
votes

If you want to add it permanently:

Add this to your ~/.bash_profile, or to ~/.zshrc if you are running MacOS Catalina or later.

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

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

33
votes

Open the ~/.bashrc file using vi/vim $ vi ~/.bashrc

Enter the following by pressing i to insert:

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

Save the file using :wq

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

source ~/.bashrc
27
votes

For those of you that run ZShell with Iterm2, add this to your ~/.zshrc file.

alias code="/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code"
16
votes

On my MAC I got it working:

add to .bash_profile

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

save and in terminal 'source .bash_profile'

Then in terminal code index.html (or whatever) will open that file in VS Code.

13
votes

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

Tip: If you want to run VSCode from the terminal, append the following to your .bashrc 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

11
votes

Steps to follow:-

  1. Open Visual Studio code application & type Command+Shift+P and type command 'install code'. Then enter it.
  2. You will see below message in Visual Studio Application :- shell command ' code' successfully installed in PATH.
  3. Now, Jump to iTerm CLI and type code .
  4. Then you will be able to redirect to Visual studio for any code change/view.

Hope this steps will be helpful for you.

10
votes

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

I simply uninstalled it first, 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.

Don't forget to restart your terminal to have the new PATH included.

10
votes

Here are the steps which I followed to make it working on MAC:

Install "Shell" extension from VSCode:

enter image description here

Restart VSCode.

Press F1 when VSCode 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 "which code" command will give you a proof the code command working now: enter image description here

8
votes

This work for me:

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

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

brew cask reinstall visual-studio-code

path turned out to be:

/usr/local/bin/code ->
'/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code'
3
votes

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 symlink to the visual studio code app 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.

2
votes

If you are using VS Code Insiders Build:

code-insiders .

With regular VS Code:

code .
1
votes

If you have trouble using the Command Palette solution, you can manually add VS 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
votes

For Mac OSX: There is a way to install Visual Studio Code through Brew-Cask.

  1. First, install 'Homebrew' from here.
  2. Now run following command and it will install latest Visual Studio Code on your Mac.

    $> brew cask install visual-studio-code

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

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

1
votes

Alternative to commandline 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 '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 '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 '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 VSCode'. 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 VSCode' menu option.
  • Click on the 'Open with VSCode' menu option.
  • The folder should get open in the Visual Studio Code application. Check image below for more info.

enter image description here

1
votes

EDIT: If this is happening on mint/ubuntu, it is likely because you installed vscode through the software manager. This will cause other problems during debugging. Instead install it using the .deb file on the vscode 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 bin file. It should end in /extra/vscode/bin/code

If you're using the 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 sym 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 / .zshrc:

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

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

1
votes

for the people who face the same problem in Windows - 10 please follow the below instructions,

https://github.com/Microsoft/vscode/issues/21957

It might be the case that, C:\Program Files (x86)\Microsoft VS Code\bin is missing in environment variables., kindly look into the following image for the solution, https://cloud.githubusercontent.com/assets/4076309/23575794/61d7cc2a-00b9-11e7-843b-bcd6f00f595f.png

1
votes

If you are on Windows and facing the same problem, have a look at this answer of @Inazense https://stackoverflow.com/a/52869803/4567504.

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 (System > Advance system settings > Advanced tab > environment variables) on system variables click on Path and click Edit and add new Path named

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

Now you are done! restart command prompt and try again

1
votes

SETUP Code . IN JUST ONE SECOND

Just Follow One simple command with following step

Step1. Open Visual Studio code application

Step2. Command+Shift+P

Step3. type command 'install code'. Then enter it.

Boom its DONE

now use the command Code . where you want to use

0
votes

Define the path of the Visual Studio in your ~/.bash_profile as follow

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

0
votes

It might be possible that you have not installed the code in your system. So, please install it first. the command is here -

sudo snap install --classic code

Details are available is here

0
votes

For code . to work in OSX terminal append code as described here https://code.visualstudio.com/Docs/setup but instead of to .bashrc, in OSX try .profile which is loaded at terminal session start.

0
votes

had this problem in kali. deleted go and reinstalled and now it works perfectly.

:)

0
votes

I've tried install add code command to PATH with Visual Studio Code's command pannel, but it's disabled after restart bash. if you want it be consolidated, just create a code file in your PATH; I create a code file in usr/local/bin and add

#!/usr/bin/env bash
function realpath() { python -c "import os,sys;print(os.path.realpath(sys.argv[1]))" "$0"; }
CONTENTS="/Applications/Visual Studio Code.app/Contents"
ELECTRON="$CONTENTS/MacOS/Electron"
CLI="$CONTENTS/Resources/app/out/cli.js"
ELECTRON_RUN_AS_NODE=1 "$ELECTRON" "$CLI" "$@"
exit $?

just replace this CONTENTS with your VS Code's installed path. and don't forget make it excuteable with chmod +x /usr/local/bin/code

0
votes

I was having the same problem. I have to add Vs Code to my applications folder. It worked without editing a file.

  1. Open Applications Folder

enter image description here

  1. Search for VS Code in your search

enter image description here

  1. Drag Vs Code to the applications folder

enter image description here

This will work for you.

0
votes

Note: with code-insider for VSCode 1.58 (June 2021), you have microsoft/vscode issue 126702 (on Windows, but also Mac)

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

This has been fixed.

0
votes

In Ubuntu 20.04

# Symlink bin command to /usr/bin
rm -f /usr/bin/code
ln -s /usr/share/code/bin/code /usr/bin/code
0
votes

For MacOS, search for VSCode application for e.g. it was in my Downloads section.

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

  • Open terminal and type vi ~/.zshrc
  • Add this line in 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 terminal and go to git code folders

You are all sorted now. Thanks