Mona Jalal
Mona Jalal

Reputation: 38255

"code ." is not working in on the command line for Visual Studio Code on OS X/Mac

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 an OS X terminal?

pwd

/Users/mona/nodejs/myExpressApp

code .

-bash: code: command not found


I ended up opening it from within Visual Code Studio by changing the workspace, but why won't that command code . part work?

Enter image description here

Upvotes: 1373

Views: 1481913

Answers (30)

Molla Manie
Molla Manie

Reputation: 9

Move or copy the Visual Studio Code application into the Applications folder works for me it was in downloads folder initiall

Upvotes: -1

Mick dK
Mick dK

Reputation: 785

On my Mac I got it working:

Add to file .bash_profile:

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

Save and in the terminal do source .bash_profile.

Then in the terminal, code index.html (or whatever) will open that file in Visual Studio Code.

Upvotes: 49

Justin Rice
Justin Rice

Reputation: 1301

For those of you that run Z shell with iTerm2, add this to your ~/.zshrc file:

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

Upvotes: 57

nullify0844
nullify0844

Reputation: 5531

If you want to add it permanently:

Add this to your ~/.bash_profile, or to ~/.zshrc if you are running macOS v10.15 (Catalina) or later. (To add, cd ~ and vim ~/.bash_profile in terminal)

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

This assumes that you have VS Code in /Applications/Visual Studio Code.app; if you have it installed in a different location ... probably move it to Applications (or modify the code to point to your custom location).

Elaborating the process to add to the file:

  1. Open terminal and go to base directory using cd ~
  2. Then, vim ~/.bash_profile inorder to edit the file
  3. Press i for making an insertion and paste the string export PATH="$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin" at the end
  4. Save and close using :wq
  5. Once this is done, paste source ~/.zshrc

Source: Visual Studio Code on macOS

Upvotes: 358

Abhinav Sinha
Abhinav Sinha

Reputation: 145

This is what has worked out for me!

  1. Remove existing code command from $PATH using Visual Studio Code.Using Visual Studio Code to remove "code" command from PATH
  1. Move into /etc/paths.d and create a new file named code into it.

     cd /etc/paths.d && touch code
    
  2. Edit the content of code as following:

    /Applications/Visual Studio Code.app/Contents/Resources/app/bin
    
  3. Restart your terminal, and you can use your code command as usual.

Upvotes: 2

Vinayakkumar
Vinayakkumar

Reputation: 6520

Make sure VS Code is installed and moved to Application section

Open VS Code

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.

Restart Terminal (Better OS)

Upvotes: 9

Mark Pieszak - Trilon.io
Mark Pieszak - Trilon.io

Reputation: 67161

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

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

For example, sometimes the VSCode application might be in your downloads folder, so make sure to move it out of there.


2. Next, open Visual Studio Code

Open the Command Palette via P and type shell command to find the Shell Command:

Use the Uninstall 'code' command in the PATH command before the "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: Visual Studio Code documentation)


👉 Remember to Restart your machine afterwards

Note: If you're running a build based off the OSS repository, you will need to run code-oss . (Dzeimsas Zvirblis' comment)


EACCES: permission denied, unlink '/usr/local/bin/code'

If you see this EACCES error, just run the "Uninstall 'code' command from PATH, then the 'install' it again, and you'll be all set!

Upvotes: 3655

Xiaotian Ye
Xiaotian Ye

Reputation: 47

In my case:

  1. Move vscode to application
  2. Try run: sudo chown -R yourUsername /usr/local/bin in terminal
  3. Open the Command Palette via ⌘+⇧+P and type shell command and use the Install 'code' command
  4. Restart Mac

Upvotes: 0

scorer
scorer

Reputation: 1434

VSCodium

If you came here from search engine and are a free/open source user like me looking for VSCodium (Free/Libre Open Source of VSCode) instead of Microsoft spyware VSCode, then here is a little change for you.

In VSCodium binary executable code is called codium.

So, when you open the Command Line Terminal via ++P and type in shell command to find the codium.

You should be able to open VSCodium using:

codium .

Upvotes: 8

Himanshu
Himanshu

Reputation: 501

Please follow below steps

  1. Open VSCode
  2. Press command + p.
  3. Type >shell command in the input box at the top.
  4. A dropdown will come, now select install code command in PATH option.

enter image description here

Upvotes: 29

Suhail Taj
Suhail Taj

Reputation: 1074

Open the ~/.bashrc file using vi or Vim:

vi ~/.bashrc

Enter the following by pressing i to insert:

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

Save and exit the file using Esc + :wq

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

source ~/.bashrc

Upvotes: 78

Ramiro Godoy
Ramiro Godoy

Reputation: 77

This is something that worked for me:

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

run this on command shell and then VS can be opened from command line

Upvotes: -1

Roytman Piccoli
Roytman Piccoli

Reputation: 1592

Go the top of VS and select menu ViewCommand Palette...

  • [macOS] Open the Command Palette via P and type shell command to find the Shell Command:
  • [Windows] Open the Command Palette via Ctrl+Shift+P and type shell command to find the Shell Command:

Type: shell command

enter image description here And install.

Upvotes: 144

Raghav
Raghav

Reputation: 9628

Here are the steps which I followed to make it work on Mac:

Install the "Shell" extension from Visual Studio Code:

Enter image description here

Restart Visual Studio Code.

Press F1 when Visual Studio Code 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 the "which code" command will give you a proof the 'code' command is working now:

Enter image description here

Upvotes: 27

xiongkailing
xiongkailing

Reputation: 57

I've tried the "Install add code" command to PATH with Visual Studio Code's command panel, but it's disabled after restarting 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 Visual Studio Code's installed path. And don't forget make it executable with chmod +x /usr/local/bin/code.

Upvotes: 0

S.Mishra
S.Mishra

Reputation: 3674

For Mac OS X, there are three ways you can enable code . to open the current folder in Visual Studio Code.

For a fresh installation

Install Visual Studio Code through Homebrew

There is a way to install Visual Studio Code through Brew-Cask.

  1. First, install Homebrew from here.

  2. Now run the following command, and it will install the latest Visual Studio Code on your Mac.

    brew cask install visual-studio-code
    

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

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

If Visual Studio Code is already installed

If Visual Studio Code is already installed then you don't have to reinstall it. You can follow any of the below two options.

Option 1: Update PATH in the Bash profile

Update your favorite Bash profile, such as ~/.bash_profile or ~/.bashrc or ~/.zshrc by exporting the app/bin path of the Visual Studio Code application. You can add the below export command to your favorite Bash profile.

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

Option 2: Using the Visual Studio Code Command Palette

We can run a shell command in the Visual Studio Code Command Palette too. To do so, we need to open the Command Palette via (⇧⌘P) and type "shell command" to find the shell command named as:

Shell Command: Install 'code' command in PATH

Press Enter to execute the above shell command.

Enter image description here

That's it.

Upvotes: 29

S.Mishra
S.Mishra

Reputation: 3674

Alternative to a command line 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 the '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 the 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 the '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 Visual Studio Code'. 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 Visual Studio Code' menu option.

  • Click on the 'Open with Visual Studio Code' menu option.

  • The folder should get open in the Visual Studio Code application. Check image below for more information.

    Enter image description here

Upvotes: 2

Erazihel
Erazihel

Reputation: 7605

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

Upvotes: 2

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

brew cask reinstall visual-studio-code

The path turned out to be:

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

Upvotes: 4

FarYang
FarYang

Reputation: 169

See Setting up Visual Studio Code

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

Upvotes: 16

Logan Tegman
Logan Tegman

Reputation: 847

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 symbolic link to the Visual Studio Code application 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.

Upvotes: 7

Josie Koay
Josie Koay

Reputation: 841

I tried this by following the documentation and it works for me:

  1. Launch Visual Studio Code

  2. Open the Command Palette (Cmd + Shift + P) and type 'shell command' to find

    Shell Command: Install 'code' command in PATH command

    Enter image description here

  3. Restart terminal

Upvotes: 16

Amit Kumar
Amit Kumar

Reputation: 1829

Use:

sudo rm /usr/local/bin/code

Open Visual Studio Code, and then press Ctrl + Shift + P

Enter image description here

And write command install code and you will get a popup. Then follow the instruction and done!

Upvotes: 60

fiki firmansyah
fiki firmansyah

Reputation: 19

It's simple. Just type this on your terminal. You can use the first or the second command:

cat << EOF >> ~/.bash_profile
export PATH="\$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"
EOF

Then press Enter.

But, since Z shell became the default shell in macOS Catalina, run the following commands to add Visual Studio Code to your path:

cat << EOF >> ~/.zprofile
export PATH="\$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"
EOF

I used the latest command, and it worked for me.

Upvotes: -2

atom217
atom217

Reputation: 987

Mac OS X

  1. Download Visual Studio Code for Mac OS X.

  2. Double-click onVSCode-osx.zip to expand the contents.

  3. Drag Visual Studio Code.app to the Applications folder, making it available in the Launchpad.

  4. Add Visual Studio Code to your Dock by right-clicking on the icon and choosing Options, Keep in Dock.

Tip: If you want to run Visual Studio Code from the terminal, append the following to your ~/.bash_profile file (~/.zshrc in case you use Z shell (executable 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.

Upvotes: 3

Mohit Tevatia
Mohit Tevatia

Reputation: 97

For macOS, search for the Visual Studio Code application. For example, it was in my Downloads section.

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

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

You are all sorted now.

Upvotes: 0

Govind Wadhwa
Govind Wadhwa

Reputation: 987

Setup code. In just one second

Just follow one simple command with the following steps:

  1. Open the Visual Studio Code application

  2. Command + Shift + P

  3. Type command 'install code'. Then enter it.

Boom, it’s done.

Now use the command Code . where you want to use it.

Upvotes: 31

In Ubuntu 20.04 (Focal Fossa):

# Symbolic link the bin command to /usr/bin
rm -f /usr/bin/code
ln -s /usr/share/code/bin/code /usr/bin/code

Upvotes: -2

VonC
VonC

Reputation: 1328002

Note: with Code Insiders for Visual Studio Code 1.58 (June 2021), you have Microsoft/Visual Studio Code issue 126702 (on Windows, but also Mac).

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

This has been fixed.

Upvotes: -1

saurabh raj
saurabh raj

Reputation: 500

Steps to follow:

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

Upvotes: 49

Related Questions