Run / Open VSCode from Mac Terminal

I would like to run / open Visual Studio Code from a Mac OSX terminal by running this code . command code . . I found instructions here:

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

Apparently I need to include this in my .bashrc , so I did this, 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 here:

~/.bashrc , which points to /Users/username/.bashrc

Which .bashrc should I edit?

+155
terminal visual-studio-code macos
May 6 '15 at
source share
11 answers

Try this one

Open Visual Studio code and press Command + Shift + P , then enter Shell in the command palette, now you can find this parameter, for example Shell Command : Install code in PATH , from the list in the command palette. Select these options.

Open VSCode via Terminal / Command Prompt

What is it.

Now open your terminal type.

 $ code . 
+714
Apr 27 '16 at 6:52
source share

If you are on a Mac OSX Maverick, it ~/.bash_profile not ~/.bashrc

Try entering the code there, close the terminal and try again. Must work

+15
May 6 '15 at 9:45
source share

I just want to get Benjamin Pasero's answer from inside his comment, as that seems like the best solution. This is the tip given on the Visual Studio Code Customization page , where it says ...

If you want to run the VS code from the terminal, add the following to the ~ / .bash_profile file (~ / .zshrc if you use zsh).

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

Now you can just enter code. in any folder to start editing files in this folder. [Or code test.txt for working with test.txt file]

+6
Oct 23 '15 at 13:24
source share

For Mac, you can do: View> Command Palette> Shell Command> "set code command on the way." I would suggest that there will be something similar for other OSs. After i do

 which code 

and he tells me that he puts it in / usr / local / bin

+6
Apr 22 '17 at 15:03
source share

I just created a file called code:

 #!/bin/bash open /Applications/Visual\ Studio\ Code.app $1 

Make it executable:

 $ chmod 755 code 

Then put this in / usr / local / bin

 $ sudo mv code /usr/local/bin 

While the file is somewhere in your path, you can open the file simply by typing: code

+1
Jan 21 '16 at 0:15
source share

To configure it, run VS Code. Then open the command palette (⇧⌘P) and enter the shell command to find the Shell command: Set the "code" command in the PATH command. enter image description here

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

0
Feb 14 '17 at 3:15
source share

I prefer to have symbolic links in the home directory, at least in this case. Here's how everything is set up for me:

 : cat ~/.bash_profile | grep PATH # places ~/bin first in PATH export PATH=~/bin:$PATH 

So I made a link to the VSCode binary as follows:

 ln -s /Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code ~/bin/code 

Now I can issue code. to any directory I wish.

0
Feb 13 '19 at 9:18
source share

Sometimes just adding a shell command doesn't work. We need to check if Visual Studio code is available in the Applications folder or not. This was the case for me.

At that moment when you download the VS code, it remains in the "Downloads" folder, and the terminal does not receive it from there. So, I manually moved my VS code to the Applications folder for access from the Terminal.

Step 1: Download the VS code, which will give out the archived folder.

Step 2: Run it, which will give exe a kind of file in the downloads folder.

Step 3: Move it to the Applications folder manually.

Step 4: Open the VS code, "Command + Shift + P" and execute the shell command.

Step 5: Reboot the terminal.

Step 6: Enter the "Code". on the terminal should work now.

0
Jul 19 '19 at 3:55
source share
 code () { if [[ $# = 0 ]] then open -a "Visual Studio Code" else echo "Opening: "$@ "/Applications/Visual Studio Code.app/Contents/MacOS/Electron" $@ fi } 

I put this in my .bash_profile I checked it and it works.

-one
Dec 07 '18 at 8:24
source share

open the crawler and go to the applications and make sure that there is vscode, then open the text in the terminal export PATH = "/ Applications / Visual Studio Code.app/Contents/Resources/app/bin"

-one
May 11 '19 at 11:13
source share

For Mac users:

One thing that made the accepted answer not work for me was that I did not drag the vs code package into the application folder

Thus, you need to drag it to the application folder and then run the command inside the code (shown below) according to the official document

  • Launch VS Code.
  • Open a command palette (⇧⌘P) and enter “shell command” to find the shell command: set the “code” command in the PATH command.
-one
Jul 02 '19 at 13:15
source share



All Articles