ChatGPT解决这个技术问题 Extra ChatGPT

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

https://i.stack.imgur.com/EbtmG.png

I reinstalled homebrew from scratch in arm64/m1 environment, and /usr/local is no longer in PATH. It's /opt/homebrew now
if you have install vscode-insider version on mac m1 than you need to run code-insiderr .

P
Peter Mortensen

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.

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.

https://i.stack.imgur.com/3gYHb.png

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)

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


this does work, but after reboot I have to do it again.
It looks like the Shell Command: Install 'code' command in PATH in VSCode just creates a symlink in /usr/local/bin/ now
Make sure you drag Visual Studio Code.app into the Applications folder. Otherwise, as @uloco said, you'll have to go through this process again after reboot.
even after dragging vscode into Applications, I've to do it every time I restart.
@jlucktay Hey thank you so much you just resolved my confusion...
P
Peter Mortensen

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.

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

Source: Visual Studio Code on macOS


Look like now code is under /usr/local/bin, so it might be better to update the answer :)
worked for me, please specify 2 more points here, you need to write the PATH variable by vim ~/.zshrc and run source ~/.zshrc to reload zsh
Worked well for me with ~/.zshrc on Catalina 10.15.7
This worked for me despite the fact that using the VS Code Install 'code' command in PATH command results in which code returning /usr/local/bin`. That was already on my PATH the true location of the executable is where you've installed VS COde.
Note this only works if Visual Studio Code is actually installed in the Applications folder. Mine was sitting in Downloads!
P
Peter Mortensen

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 the file using :wq

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

source ~/.bashrc

this worked for me, I was getting an error "cannot execute binary file" after my path probably got messed somehow - but now it won't open any files if i do code filename, just opens an empty window of VS code
this worked for me. thank you for the help
Does this still work? The default shell changed to Z shell in macOS v10.15 (Catalina).
P
Peter Mortensen

Steps to follow:

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


this worked for me. thanks! For mac users, make sure vs code is in the applications folder.
Show us instead of telling us. I'm a new user and I don't even now where the applications folder is, and no idea of to "make sure".
thanks it worked
No matching commands for me.
Simple yet effective way. Thank you.
P
Peter Mortensen

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"

Thanks, zsh user here too, this was just what I needed, though my path was a little different in order to get it to work: alias code="/Applications/VisualStudioCode.app/Contents/Resources/app/bin/code"
It didn't work for me. Now it doesn't even recognise code anymore
@AMMA I would think it has a lot to do with where you installed your VSCode.
Z shell became the default shell with macOS v10.15 (Catalina)—released about half a year after this answer.
P
Peter Mortensen

Use:

sudo rm /usr/local/bin/code

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

https://i.stack.imgur.com/A7lNa.png

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


this is just temporary solution, because after reboot need to do it again.
does not work for mac os monterey
Okay, let me update a permanent solution for any mac os version.
R
Roytman Piccoli

Go the top of VS and select menu View → Command Palette...

Type: shell command

And install.


this one works well. thanks.
This is nearly identical to Josie Koay's answer.
Is "shell command" literal or not? It doesn't seem to do anything—"No matching commands" (version 1.66 of Visual Studio Code. On Linux, though). Is it dependent on some extension being installed? What version of Visual Studio Code was it tried on?
P
Peter Mortensen

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.


P
Peter Mortensen

Setup code. In just one second

Just follow one simple command with the following steps:

Open the Visual Studio Code application Command + Shift + P Type command 'install code'. Then enter it.

Boom, it’s done.

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


Worked like a Charm
I don't think "Code ." will work for the question here (Mac). It may work on a Windows system. On a Linux system I get "Command 'Code' not found".
P
Peter Mortensen

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.

First, install Homebrew from here. 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.

https://i.stack.imgur.com/ki6FZ.png

That's it.


P
Peter Mortensen

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

Install the "Shell" extension from Visual Studio Code:

https://i.stack.imgur.com/ja00i.png

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:

https://i.stack.imgur.com/DUBNF.png

That will give you the following message: Shell command 'code' successfully installed in PATH.

https://i.stack.imgur.com/tqdaR.png

Running the "which code" command will give you a proof the 'code' command is working now:

https://i.stack.imgur.com/PIlMU.png


Please review Why not upload images of code/errors when asking a question? (it is not only about questions), and take the appropriate action. Thanks in advance.
This worked seemlessly for me, thanks
P
Peter Mortensen

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

Launch Visual Studio Code Open the Command Palette (Cmd + Shift + P) and type 'shell command' to find Shell Command: Install 'code' command in PATH command Restart terminal


How is this different from the accepted answer?
P
Peter Mortensen

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


Yes. The source ~/.bashrc is missing from the official docs.
P
Peter Mortensen

This works for me:

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

Why does it work? What is the gist of it? An explanation would be in order. From the Help Center: "...always explain why the solution you're presenting is appropriate and how it works". Please respond by editing (changing) your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today).
I can't speak for OP, but a version of this worked for me because a ls -lah /usr/local/bin showed that code was in there and linked to a long directory path...that didn't exist. So I just deleted it and made a new link to the /Application folder path that did exist. Now when the shell looks for code in /usr/local/bin, it find it, and not a broken link.
P
Peter Mortensen

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

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

Restart your terminal to have the new PATH included.


P
Peter Mortensen

If you are using Visual Studio Code Insiders build:

code-insiders .

With regular Visual Studio Code:

code .

Thanks a billion! ;D You have just saved the day! This answer should be on the very top :))) thanks!
P
Peter Mortensen

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.


"/Applications/Visual\ Studio\ Code.app/Contents/MacOS/Electron" should be the target
P
Peter Mortensen

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'

P
Peter Mortensen

Mac OS X

Download Visual Studio Code for Mac OS X. Double-click onVSCode-osx.zip to expand the contents. Drag Visual Studio Code.app to the Applications folder, making it available in the Launchpad. 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.


P
Peter Mortensen

If this is happening on Linux Mint or Ubuntu, it is likely because you installed Visual Studio Code through the software manager. This will cause other problems during debugging. Instead install it using the .deb file on the Visual Studio Code 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 Code binary file. It should end in /extra/vscode/bin/code

If you're using the Linux 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 symbolic 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 or .zshrc file:

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

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


Yes, but the question was about OS X/macOS.
P
Peter Mortensen

If you are on Windows and facing the same problem, have a look at Inazense's answer, Visual Studio Code - "Shell Command: Install 'code' command in PATH command.".

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.

Open Environment Variables (System → Advanced system settings → Advanced tab → Environment variables). In system variables, click on Path and click Edit and add a new path named:

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

Now you are done! Restart the command prompt and try again.


Yes, but the question is about OS X/macOS.
P
Peter Mortensen

I was having the same problem. I had to add Visual Studio Code to my applications folder. It worked without editing a file.

Open the Applications folder Search for Visual Studio Code in your search Drag Visual Studio Code to the Applications folder

This will work for you.


P
Peter Mortensen

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

P
Peter Mortensen

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.

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.


P
Peter Mortensen

Define the path of the Visual Studio Code in your ~/.bash_profile file as follows:

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

P
Peter Mortensen

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

sudo snap install --classic code

Details are available here.


P
Peter Mortensen

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.


P
Peter Mortensen

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

What do you mean by "bin command"? E.g., is "bin" literal?
An explanation would be in order. E.g., what is the idea/gist? From the Help Center: "...always explain why the solution you're presenting is appropriate and how it works". Please respond by editing (changing) your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today).
P
Peter Mortensen

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.


What is the "Downloads section"? The web browser's download folder? Or something Mac-specific?
P
Peter Mortensen

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.