[macos] "code ." Not working in Command Line for Visual Studio Code on OSX/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 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

This question is related to macos visual-studio-code

The answer is


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


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


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'

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

:)


If you are using VS Code Insiders Build:

code-insiders .

With regular VS Code:

code .

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


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


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

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


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


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.


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.


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.


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


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


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.


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"

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.


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"


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


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.


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.


This work for me:

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

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


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

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