Reputation: 57562
I'd like to run / open Visual Studio Code from the Mac OSX Terminal by running this command code .
. I found instructions here:
https://code.visualstudio.com/Docs/setup
Apparently I need to include this in my .bashrc
file, so I did, but to no avail.
code () {
if [[ $# = 0 ]]
then
open -a "Visual Studio Code"
else
[[ $1 = /* ]] && F="$1" || F="$PWD/${1#./}"
open -a "Visual Studio Code" --args "$F"
fi
}
I edited the .bashrc
file here:
~/.bashrc
which points to /Users/username/.bashrc
Which .bashrc
should I be editing?
Upvotes: 852
Views: 682502
Reputation: 1
Do as the top answer suggests and install code to path using command palette.
This should install code to path and you should be able to write where code
and get the path to it's binary
which is /usr/local/bin/code
for me, now open ~/.zshrc by writing code ~/.zshrc
and add the line export PATH="/usr/local/bin/code":$PATH
(or where ever it was in your case) to your zshrc
This should add code to your path permanently.
Upvotes: 0
Reputation: 1
The link in /usr/local/bin
gets overwritten if you try out cursor. Plus, if you use VSCode Insiders, the link it generates is /usr/local/bin/code-insiders
. Hence, if it doesn't work after using the shortcut withing VSCode, then look into that directory, and e.g.
sudo rm /usr/local/bin/code
then
sudo ln -s /usr/local/bin/code-insiders /usr/local/bin/code
or
sudo ln -s "/Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/bin/code" /usr/local/bin/code
Upvotes: -1
Reputation: 153
********** UPDATED FOR 2024 *************************
Add the following line to your ~/.zshrc
file:
alias code="open -a 'Visual Studio Code'"
re-source your .zshrc
file by running the following command in your terminal:
$ source ~/.zshrc
Usage:
$ code my-project-folder/
Upvotes: 15
Reputation: 1482
To set up the Visual Studio Code path permanently on Mac OS, just open .bash_profile
using the following command on the terminal:
open -t .bash_profile
Then add the following path to .bash_profile
:
code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;}
Save the .bash_profile
file and quit the terminal. Then reopen the terminal and type code .
to open Visual Studio Code.
Upvotes: 15
Reputation: 1127
follow some simple steps :
Now you can use from terminal by typing
$ code .
Upvotes: 49
Reputation: 71
In my case, the Shell Install PATH does not work. So, I ran the Uninstall command and then run the install command again. Now it works well.
Upvotes: 1
Reputation: 181
Open Visual Studio Code application:
> Shell
in the command paletteOpen Terminal application:
code .
in mac terminalUpvotes: 18
Reputation: 5934
you can manually add VS Code to your path, to do so run the following commands:
cat << EOF >> ~/.bash_profile
# Add Visual Studio Code (code)
export PATH="\$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"
EOF
Start a new terminal to pick up your .bash_profile changes.
Note: The leading slash \ is required to prevent $PATH from expanding during the concatenation. Remove the leading slash if you want to run the export command directly in a terminal.
Note: Since zsh became the default shell in macOS Catalina, run the following commands to add VS Code to your path:
cat << EOF >> ~/.zprofile
# Add Visual Studio Code (code)
export PATH="\$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"
EOF
Upvotes: 0
Reputation: 86
I follow this step it work for me .π
first open VSC . open terminal of VSC.
Press cmd+shift+p
uninstall path . Give permission to it.
Press cmd+shift+p
install path .
then open Mac terminal navigate to root project file type
code . in root folder It will open VSC . :)
Upvotes: 4
Reputation: 36177
According to the docs on Launching from the command line:
Shell
in command paletteShell Command: Install code in PATH
from suggested listThat's it.
Now open your terminal type.
$ code .
Many Mac users find this is forgotten and needs to be re-applied after any restart. This may happen if MacOS has applied the quarantine attribute to VS Code, which the OS uses for the "Are you sure?" notice applied on first using apps downloaded from the internet.
To check if this attribute is applied, look for com.apple.quarantine
in the list returned by this command (changing the path if that's not where you installed it):
xattr "/Applications/Visual Studio Code.app"
If that does return com.apple.quarantine
, you can remove the attribute using the same command with the -d
flag (alongside -r
to recursively remove it from all contained files and sudo
to allow the change):
sudo xattr -r -d com.apple.quarantine "/Applications/Visual Studio Code.app"
...then do Shell Command : Install code in PATH
as above after the attribute has been removed, and it should persist after restart.
Credit: derflounder.wordpress.com article linked to by RicardoVallejo in this comment.
Upvotes: 2749
Reputation: 91
Yo do this:
Upvotes: 8
Reputation: 49520
For macOS 12.0 and above:
open ~/.zshrc
code
, Paste below:alias code='open -a "Visual Studio Code"' # open file or folder in VSCode e.g. code ~/.zshrc
Now you can open the current folder e.g. code .
or any other file/folder by providing its path.
Profit
PS: You can add as many aliases as needed to open a file/folder with different editors. Just mention the editor's name in the alias. For example, open file/folder with sublime text:
alias subl='open -a "Sublime Text"' # open file or folder in sublime e.g. subl ~/.zshrc
And use it like subl .
Upvotes: 23
Reputation: 11
alias code="/Applications/Visual\ Studio\ Code\ 2.app/Contents/Resources/app/bin/code $1"
the alias to the vs code's bin file with parameters works well
you can do code .
after having sourced your bash file
Upvotes: 1
Reputation: 429
Open VSCode, press Command + Shift + P, type Shell in command palette, Select that option => Install code in PATH from suggested list in command palette.
Upvotes: 23
Reputation: 779
To setup path permanently for mac users;
open ~/.zshrc
using the below command
vi ~/.zshrc
Add the following path
export PATH="$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"
And source it using below command
source ~/.zshrc
Now close the terminal and reopen and run code .
command should work properly.
Upvotes: 73
Reputation: 83
Since, default shell is zsh
in macOS, you can try this:
cat << EOF >> ~/.zshrc
# Add Visual Studio Code (code)
export PATH="\$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"
EOF
This will add a path to your VS Code, restart your terminal and voila, you're good to go.
code example.py
Upvotes: 1
Reputation: 181
I moved VS Code from Downloads folder to Applications, and then i was able to run code in the terminal. I guess, it might help you too.
Upvotes: 2
Reputation: 31
add below snipped in your bash profile -
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;}
Upvotes: 2
Reputation: 3576
Somehow using Raja's approach worked for me only once, after a reboot, it seems gone.
To make it persistent across Mac OS reboot, I added this line into my ~/.zshrc
since I'm using zsh:
export PATH=/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin:$PATH
then
source ~/.zshrc
now, I could just do
code .
even after I reboot my Mac.
Upvotes: 14
Reputation: 369
I just made a symbolic link from the "code" program supplied in the Visual Studio Code.app bundle to /usr/local/bin (a place where I prefer to put stuff like that and which is already in my path on my machine).
You can make a symbolic link using ln -s like this:
ln -s /Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code /usr/local/bin/code
Upvotes: 3
Reputation: 1488
How about a simple Bash alias that you stick in your .bash_profile ?
alias code="open -a /Applications/Visual\ Studio\ Code.app"
To open the current directory:
code .
Upvotes: 7
Reputation: 213
Sometimes, just adding the shell command doesn't work. We need to check whether visual studio code is available in "Applications" folder or not. That was the case for me.
The moment you download VS code, it stays in "Downloads" folder and terminal doesn't pick up from there. So, I manually moved my VS code to "Applications" folder to access from Terminal.
Step 1: Download VS code, which will give a zipped folder.
Step 2: Run it, which will give a exe kinda file in downloads folder.
Step 3: Move it to "Applications" folder manually.
Step 4: Open VS code, "Command+Shift+P" and run the shell command.
Step 5: Restart the terminal.
Step 6: Typing "Code ." on terminal should work now.
Upvotes: 17
Reputation: 8568
For Mac users:
One thing that made the accepted answer not work for me is that I didn't drag the vs code package into the applications folder
So you need to drag it to the applications folder then you run the command inside vs code (shown below) as per the official document
Upvotes: 13
Reputation: 158
open finder and go to applications and make sure that vscode exists there ,then open type in terminal export PATH="/Applications/Visual Studio Code.app/Contents/Resources/app/bin"
Upvotes: 0
Reputation: 10280
I prefer to have symlinks in the home directory, in this case at least. Here's how I have things setup:
: cat ~/.bash_profile | grep PATH
# places ~/bin first in PATH
export PATH=~/bin:$PATH
So I symlinked to the VSCode binary like so:
ln -s /Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code ~/bin/code
Now I can issue code .
in whichever directory I desire.
Upvotes: 1
Reputation: 71
code () {
if [[ $# = 0 ]]
then
open -a "Visual Studio Code"
else
echo "Opening: "$@
"/Applications/Visual Studio Code.app/Contents/MacOS/Electron" $@
fi
}
I put that into my .bash_profile
I tested it and it works.
Upvotes: -1
Reputation: 8904
For Mac you can do : View > Command Palette > Shell command > "install code command in path". I'd assume there would be something similar for other OS's. After I do
which code
and it tells me it put it in /usr/local/bin
Upvotes: 19
Reputation: 6168
If you are on Mac OSX Maverick,
it's ~/.bash_profile
not ~/.bashrc
Try putting the code in there, close the terminal and then try again. Should be working
Upvotes: 22
Reputation: 67
I simply created a file called code:
#!/bin/bash
open /Applications/Visual\ Studio\ Code.app $1
Make it executable:
$ chmod 755 code
Then put that in /usr/local/bin
$ sudo mv code /usr/local/bin
As long as the file sits someplace that is in your path you can open a file by just typing: code
Upvotes: 1
Reputation: 17525
I just want to pull out Benjamin Pasero's answer from inside his comment as it seems the best solution. It is the tip given on the Setting up Visual Studio Code page where it says ...
If you want to run VS Code from the terminal, append the following to your ~/.bash_profile file (~/.zshrc in case you use 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. [Or code test.txt
to go to work on the test.txt
file]
Upvotes: 101