How to open Visual Studio Code from the command line on OSX?
Asked Answered
P

33

565

The docs mention an executable called code, but I'm not sure where I can find that so I can put it on my path. The zip I downloaded from the VSCode site did not include any such executable. (I am able to run the .app just fine.)

Is this a Windows-only thing?

Photoemission answered 30/4, 2015 at 14:56 Comment(2)
type in command pellet (cmd+shift+P): Type shell command to find Shell Command: Install 'code' command in PATH commandCarrie
@CharlieParker When I type code, I am not able to find that shell command.Tammietammuz
H
974

From the Visual Studio Code Setup page:

Tip: If you want to run VS Code from the terminal by simply typing 'code', VS Code has a command, Shell Command: Install 'code' command in PATH, to add 'code' to your $PATH variable list.

After installation, launch VS Code. Now open the Command Palette (F1 or ++P on Mac) and type shell command to find the Shell Command: Install 'code' command in PATH command.

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.

Hoppe answered 30/4, 2015 at 15:12 Comment(8)
On a side note, vscode ver 0.10.11 didn't add anything to my PATH on win 10, like that page says it would. No biggie.Williwaw
You can add an option -r to open it in a new tab in old window: code -r <filename>. Normally, that is what you want.Greenness
I'm using macOS Sierra 10.12.6. The code command fails to launch Visual Studio Code after I restart my laptop.Queston
When I do this, VS Code creates a symlink called /usr/local/bin/code which links to an executable in a temporary directory under /private/var/folders. After restarting my computer, that directory is deleted so the symlink goes away. I made a more persistent symlink with: sudo ln -s /Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code /usr/local/bin/code. Assuming /usr/local/bin is in your PATH this will survive a restart.Canoewood
code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;} you can try adding this to /.zshrc if it doesn't work out.Foreboding
Opening command palette is now cmd + shft+ pLenhart
For the Mac users, please put your VS code editor application in the Applications directory otherwise the code command won't work after rebootVolkan
After pressing f1 and typing shell command, there are no options to click in the drop down. Any ideas?Afghanistan
R
428
  1. Download, install and open Visual Studio Code.

  2. Open the Command Palette ( + + P on Mac) OR ViewCommand Palette

  3. Type shell command to find Shell Command: Install 'code' command in PATH command

  4. Install it


Here's a complimentary GIF.

Install Code on Command line

After that, you can use code or code . in the terminal.

code

If you'd like to go a little bit further and learn a couple of great tips/tricks for using the VSCode CLI, I made a YouTube video on my workflows.

Rhomb answered 21/9, 2017 at 11:51 Comment(11)
For whatever reason (⌘ + ⇧ + P ) was not working but F1 did it. Thanks!Kelcey
After (⇧⌘P), just an example, use the View:Toggle Integrated Terminal command.Juniper
^That's not what is being asked here.Rhomb
(⌘ + ⇧ + P ) didn't work for me, I had to use View -> Command palette to have data appear in the command palette searchBriefing
^You must be on windows or linux. Use <kbd>Ctrl</kbd> instead of <kbd>⌘</kbd> there.Rhomb
I believe (⌘ + ⇧ + P ) is for if you are using the Sublime Text key mappings. Worked for me 👍Otherwise you can just open the command palette from the top menu bar > View > Command Palette.Westleigh
I have tried this and worked. However, Every time I restart my machine, code . is not recognized anymoreConditional
That's weird. File that as an issue on github.com/Microsoft/vscode/issuesRhomb
code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;} you can try adding this to /.zshrc if it doesn't work out.Foreboding
May I ask you where is the file which the PATH is added?Enclose
What if "Shell Command: Install 'code' command in PATH command" doesn't show up? Only two options populate: 1. (Preview) PowerShell Command Explorer: Focus on PowerShell Commands View 2. View: Show (Preview) PowerShell Command ExplorerAfghanistan
D
57

If you want to open a file or folder on Visual Studio Code from your terminal, iTerm, etc below are the commands which come as default when you install Visual Studio Code

To open Visual Studio Code from command line

code --

To open the entire folder/directory

code .

To open a specific file

code file_name
eg:- code index.html
Delaney answered 24/1, 2017 at 6:16 Comment(0)
N
40

We since updated the script to the following syntax to support multiple files and folders as arguments and to fix an issue with not detecting the current working directory properly:

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

Update for our VS Code 1.0 release:

Please use the command Install 'Code' command in path or Install 'code-insiders' command in path from the command palette (View | Command Palette) to make Code available to the command line.

Newmint answered 3/6, 2015 at 18:50 Comment(2)
Is there a way to make this work with sudo, such as: sudo code. I'm using VSCode to work on some NPM modules and need sudo to overwrite.Incredible
@BenjaminPasero see my answer for a version which works with mingw32. Please add to VSCode setup docs if possible.Corpus
G
29

This was the tutorial I was looking for in this thread. It shows the way to open files in Visual Studio Code by writing code.

1.- Open the file

Bash

open ~/.bash_profile 

Terminal OS

open ~/.zshrc

2.- Add in your file the :

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

3.- Reinitialize terminal and try in the folder you want to open

code .

4.- Then you can use it as shown in this comment: https://mcmap.net/q/73312/-how-to-open-visual-studio-code-from-the-command-line-on-osx

Germaun answered 4/7, 2018 at 18:3 Comment(5)
This was the only solution that worked for me. VS Code's CMD+SHIFT+P was not maintaining state.Jennefer
Worked perfect!Leyte
code ~/.zshrc worked for me thanksLuculent
This is a good backup solution. For some reason the built in shell command was failing because of an access error, but this did it for me. Thanks!Frontlet
Thanks! This is the best solution for the Mac M1 chip users. When I tried the 'export code to path' method, I would only get an "illegal hardware instruction" error. This solution works beautifully.Delacruz
C
28

For me on Macbook Book Pro 2019 MacOS version 10.15.6, shortcut to open command palette in VSCode was Shift + Command + P.

On opening it one has to just write install code and press enter.

enter image description here

After that just open the terminal and type code your vscode will start opening.

Camise answered 5/8, 2020 at 15:13 Comment(3)
worked for me in version 11 (2015)Jeanicejeanie
This should be selected as the correct answerDownatheel
Thanks Thomas, it doesn't matter as for as it helps people. Upvote does the job well :)Camise
B
13

I have a ~/bin/code shell script that matches the command @BengaminPasero wrote.

#!/bin/bash
VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $*

I prefix ~/bin: to my $PATH which allows me to add a bunch of one off scripts without polluting my ~/.bash_profile script.

Blurb answered 22/9, 2015 at 17:32 Comment(2)
in bash_profile can add a function. <br> function code { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* }Presentable
@Presentable I specifically stated that I do this to avoid bloating my bash_profile... Also, this answer and the original question are from over 4 years ago, before the option to add to the path was in the application.Blurb
M
11

Note: Only for Windows Users.

As many folks already suggested ways to open code from command prompt using code . command. This will only open Visual Studio Code Stable build. But If you have downloaded Visual Studio Code Insider build/version (Which has all latest build/features but unstable version) then you need to follow below instructions in windows :

  • Go to Control Panel\System and Security\System. Click on Advanced System Settings enter image description here
  • Click on Environment Variables enter image description here
  • Under System Variables tab, Click on Edit for Path Variable enter image description here
  • Add a new path C:\Users\tsabu\AppData\Local\Programs\Microsoft VS Code Insiders\bin (or) C:\Program Files\Microsoft VS Code Insiders\bin based on location at which you have installed vscode insider in your machine. enter image description here

    Open a new command prompt and type code-insiders . to open vscode-insider build/version
Monocular answered 31/8, 2018 at 5:3 Comment(1)
The question is regarding OSX not windows, I don't think this is a relevant answer. But as you got 6+, so that means it is still helping some people out there.Camise
N
10

After opening VSC and pressing (Command + Up + P) I tried typing in "shell command" and nothing came up. In order to get "Shell Command: Install 'code' command in PATH command" to come up, you must do the following:

  1. Press (Command, Up, P)

  2. Type > (this will show and run commands)

  3. Then type Shell Command: Install 'code' command in PATH command. It should then come up.

    Once you click it, it will update and you should be good to go!

MacOS X Launch from Command Line docs

Navarrette answered 19/7, 2018 at 17:14 Comment(0)
S
8

On OSX Mavericks I created a bash script named vscode (adapted from the .bashrc in VSCode Setup) in ~/bin:

#!/bin/bash

if [[ $# = 0 ]]
then
    open -a "Visual Studio Code"
else
    [[ $1 = /* ]] && F="$1" || F="$PWD/${1#./}"
    open -a "Visual Studio Code" --args "$F"
fi

vscode <file or directory> now works as expected.

Spent answered 2/5, 2015 at 10:45 Comment(2)
This script will work if Code is closed, but if it is already open, it just brings it forward and doesn't switch to the new directory. Is there a way to correct that?Passive
You could use open -n -a ... to open a new instance.Compressed
J
8

If you are using VS Code Insiders:

code-insiders .

If you are using VS Code:

code .
Johnathanjohnathon answered 30/7, 2020 at 15:43 Comment(0)
M
7

If you install Your vs code in Download folder you need to move the VS code to Application folder then open the vs code ,then press shift + command + p after you will see enter image description here the below image. Then you need to type code . Now you are good to go.

Mcgarry answered 3/12, 2019 at 3:16 Comment(0)
K
7

Steps to run code . command in mac to start the VSCode app -

  1. Open VSCode
  2. Open command pallet(Cmd+Shift+P)
  3. Enter Shell Command: Install 'code' command in PATH and select
  4. You will get the notification saying Shell command 'code' successfully installed in PATH.
  5. Restart the terminal and enter code .
  6. This will open VSCode from the current folder files in it.
Kora answered 18/7, 2022 at 9:56 Comment(0)
S
6

Try this one

Open Visual Studio Code and press Command + Shift + P then type Shell in command palette now you are able to find this option like Shell Command : Install code in PATH from suggested list in command palette. Select that options.

Open VSCode via Terminal/Command Prompt

That's it.

Now open your terminal type.

$ code .

Sulfanilamide answered 16/5, 2020 at 9:22 Comment(0)
E
5

You can use the vscode: protocol that Visual Studio Code defines:

open vscode://file/full/path/to/project/or/file

You can also use

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

if you do not fancy modifying your path

Eupepsia answered 24/11, 2017 at 12:23 Comment(0)
C
4

I discovered a neat workaround for mingw32 (i.e. for those of you using the version of bash which is installed by git-scm.com on windows):

code () { VSCODE_CWD="$PWD" cmd //c code $* ;}
Corpus answered 16/8, 2015 at 22:18 Comment(2)
FYI: You should probably ask/answer your own separate question for Windows specifically for this, if there isn't already one.Blurb
Launch "git bash" (i.e. mingw32) and simply type it at the bash command prompt and hit enter. Now to use it cd /c/some/path and then code . If you want it to be set automatically every time then you run git bash then add it to your ~/.bash_profileCorpus
N
4

its very simple:

Launching from the Command Line

You can also run VS Code from the terminal by typing 'code' after adding it to the path:

Launch VS Code. Open the Command Palette (⇧⌘P) and type 'shell command' to find the Shell Command: Install 'code' command in PATH command.

source

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

Ninfaningal answered 27/9, 2018 at 7:59 Comment(0)
J
4

This is what worked for me on Mac OS Catalina -- found here (thanks, Josiah!)

If you're on Mac OS Catalina, you need to edit your .zprofile instead of .bash_profile.

  1. Edit your ~/.zprofile file: vim ~/.zprofile
  2. Add the following code in it, on it's own line: code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;}
  3. Save the file: :wq
  4. Re-run the updated file: source ~/.zprofile.
  5. Test that running code . opens your current folder in VS Code!
Jasperjaspers answered 8/6, 2020 at 17:33 Comment(1)
On Mac OS Monterey also its .zprofile instead of .bash_profile.Rainout
O
3

I ran: open -a "Visual Studio Code" [folder-name] to open a folder with my Visual Studio Code application. Folder name is optional if you just want to open the application. Not sure if this is exactly your use-case, but hope this helps!

Ossuary answered 22/3, 2017 at 16:9 Comment(0)
V
3

In my case I had to use an alias:

alias code="/<PATH TO VSCODE>/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code"

You can add this alias in your ~/.bash_profile.

Volitive answered 28/5, 2021 at 22:55 Comment(0)
S
3

I added this to my ~/.profile

alias vscode='/Applications/Visual\ Studio\ Code.app/Contents/MacOS/Electron'

then

. ~/.profile

afterwards I can just do

 vscode

from the terminal

Somerset answered 29/10, 2021 at 9:52 Comment(2)
Thanks for the comment, that worked for me. However, please correct the typo. There needs to be a space after the second backslash.Plasmasol
Worked for me and I modified it just now to have the space thanks @PlasmasolCullum
N
2

Added this to /usr/local/bin/code, you might have to modify the path if they are different.

#!/usr/bin/env bash

CONTENTS="/Applications/Visual Studio Code.app/Contents"
ELECTRON="$CONTENTS/MacOS/Electron"
CLI="$CONTENTS/Resources/app/out/cli.js"
ATOM_SHELL_INTERNAL_RUN_AS_NODE=1 "$ELECTRON" "$CLI" "$@"
exit $?

Make executable afterwards

sudo chmod +x /usr/local/bin/code
Naji answered 29/7, 2016 at 12:45 Comment(1)
Many thanks this is the only way that it works on my machine. I noticed that the editor would open the file $CONTENTS/Resources/app/out/cli.js itself. I removed the references to CLI and it behaves as expected.Lefthand
M
2

I had this issue because of VS Code Insiders. The path variable was there but I needed to rename the code-insiders.cmd inside to code.cmd .

Maybe this is useful to someone.

Monck answered 31/7, 2016 at 8:43 Comment(0)
Z
2

For windows Users just type in

>code .

More commands here https://code.visualstudio.com/docs/editor/command-line

Zhukov answered 18/10, 2017 at 2:19 Comment(0)
I
2

If you're using visual code insiders and you want to open a file or folder in Visual Studio Code insiders from your terminal or any other command line tool then you can refer to the commands below which come by default inside visual studio code insider.

To open Visual Studio Code from command line

code-insiders --

To open the entire folder/directory

code-insiders .

To open a specific file

code-insiders file_name

eg:- code index.html

Ionosphere answered 1/2, 2021 at 12:1 Comment(0)
N
2

VSCode now supports it out of the box with version 1.58. Just type:

$ cd path/to/your/directory
$ code .
Nolly answered 19/7, 2021 at 9:9 Comment(0)
F
2

Currently, for macOS, the build-in Command Pallete ++P routine Shell Command: Install 'code' command in PATH command will create a symbolic link from /usr/local/bin/code to the Visual Studio Code.app executable code. Or, for VSCodium, from /usr/local/bin/codium to the VSCodium.app executable codium.

The code (or codium) symbolic link can be created directly from the command line:

### For Visual Studio Code
sudo ln -s  \
  "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code" \
   /usr/local/bin/code

### For VSCodium
sudo ln -s  \
  "/Applications/VSCodium.app/Contents/Resources/app/bin/codium" \
   /usr/local/bin/codium

The user alert "successfully installed in PATH" can be a bit misleading since the PATH variable is not actually modified by the osascript in this case. By default, /usr/local/bin is already on the macOS $PATH for the default shell zsh.

enter image description here

Note: A ln -s symbolic link is persistent and available across shells (bash, zsh, …). This approach avoids the need to tweak $PATH, add an alias or define some method code () {…}.

ls -l /usr/local/bin/cod*
# lrwxr-xr-x  1 root  wheel  68 Nov 13 11:56 /usr/local/bin/code -> /Applications/Visual Studio Code.app/Contents/Resources/app/bin/code
# lrwxr-xr-x  1 root  wheel  60 Nov 13 11:53 /usr/local/bin/codium -> /Applications/VSCodium.app/Contents/Resources/app/bin/codium

Also, alternate link names (e.g. vscode, vscodium) can be used, if desired.

Configuration Used:

  • macOS Ventura 13.6, Sonoma 14.1.1
  • Visual Studio Code 1.84.2
  • VSCodium 1.84.2
Fornof answered 13/11, 2023 at 20:38 Comment(0)
U
1

The instruction given at VS Code Command Line for launching a path are incorrect; the leading colon shown in the example doesn't work. However, launching with a backslash terminated directory name opens the specified directory as expected.

So, for example,

code C:\Users\DAVE\Documents\Programming\Angular\StringCalculator\src\

opens the Visual Studio Code editor in directory C:\Users\DAVE\Documents\Programming\Angular\StringCalculator\src.

Important: The terminal backslash, though optional, is useful, as it makes clear that the intend is to open a directory, as opposed to a file. Bear in mind that file name extensions are, and always have been, optional.

Beware: The directory that gets appended to the PATH list is the \bin directory, and the shell command code launches a Windows NT Command script.

Hence, when incorporated into another shell script, code must be called or started if you expect the remainder of the script to run. Thankfully, I discovered this before my first test of a new shell script that I am creating to start an Angular 2 project in a local Web server, my default Web browser, and Visual Studio Code, all at once.

Following is my Angular startup script, adapted to eliminate a dependency on one of my system utilities that is published elsewhere, but not strictly required.


@echo off

goto SKIPREM

=========================================================================

Name:               StartAngularApp.CMD

Synopsis:           Start the Angular 2 application installed in a specified
                     directory.

Arguments:          %1 = OPTIONAL: Name of directory in which to application
                          is installed

Remarks:            If no argument is specified, the application must be in
                    the current working directory.

                    This is a completely generalized Windows NT command
                    script (shell script) that uses the NPM Angular CLI to
                    load an Angular 2 application into a Node development
                    Web server, the default Web browser, and the Visual
                    Studio Code text editor.

Dependencies:       Unless otherwise specified in the command line, the
                    application is created in the current working directory.

                    All of the following shell scripts and programs must be
                    installed in a directory that is on the Windows PATH
                    directory list.

                    1)  ShowTime.CMD

                    2)  WWPause.exe

                    3)  WWSleep.exe

                    4)  npm (the Node Package Manager) and its startup 
                        script, npm.cmd, must be accessible via the Windows
                        PATH environment string. By default, this goes into
                        directory C:\Program Files\nodejs.

                    5)  The Angular 2 startup script, ng.cmd, and the Node
                        Modules library must be installed for global access.
                        By default, these go into directory %AppData%\npm.

Author:             David A. Gray

Created:            Monday, 23 April 2017

-----------------------------------------------------------------------
Revision History
-----------------------------------------------------------------------

Date       By  Synopsis
---------- --- --------------------------------------------------------
2017/04/23 DAG Script created, tested, and deployed.
=======================================================================

:SKIPREM

echo BOJ %~0, version %~t0
echo.
echo -------------------------------------------------------
echo Displaying the current node.js version:
echo -------------------------------------------------------
echo.
node -v
echo.
echo -------------------------------------------------------
echo Displaying the current Node Package Manager version:
echo -------------------------------------------------------
echo.
call npm -v
echo.
echo -------------------------------------------------------
echo Loading Angular starter application %1
echo into a local Web server, the default Web browser, and
echo the Visual Studio Code text editor.
echo -------------------------------------------------------
echo.

if "%1" neq "" (
    echo.
    echo -------------------------------------------------------
    echo Starting the Angular application in directory %1
    echo -------------------------------------------------------
    echo.
    cd "%~1"
    call code %1\src\
) else (
    echo.
    echo -------------------------------------------------------
    echo Starting the Angular application in directory %CD%
    echo -------------------------------------------------------
    echo.
    call code %CD%\src\
)

call ng serve --open

echo.
echo -------------------------------------------------------
echo %~nx0 Done!
echo -------------------------------------------------------
echo.
Pause
Uprising answered 25/4, 2017 at 4:9 Comment(0)
S
1

$> open -a "Visual Studio Code" [file-name]

Semen answered 4/6, 2021 at 8:54 Comment(0)
J
0

link your currentily folders to vscode.

Windows Registry Editor Version 5.00

; Directory\Background\shell => on empty space

[HKEY_CLASSES_ROOT\Directory\Background\shell\vscode]
"Icon"="C:\\current-folder-vscode\\Code.exe,0"
@="VsCode"

[HKEY_CLASSES_ROOT\Directory\Background\shell\vscode\command]
@="C:\\current-folder-vscode\\Code.exe ."

; Directory\shell => on a folder

[HKEY_CLASSES_ROOT\Directory\shell\vscode]
@="VsCode"
"Icon"="C:\\current-folder-vscode\\Code.exe,0"

[HKEY_CLASSES_ROOT\Directory\shell\vscode\command]
@="C:\\current-folder-vscode\\Code.exe ."
Jaunitajaunt answered 8/4, 2020 at 18:29 Comment(1)
OP wanted a OSX solution.Napper
S
0

If you install VS CODE using snap. You will need to add /snap/bin in your PATH environment variable. so - open your .bashrc or .zshrc and add /snap/bin in your PATH environment variable

reload terminal, and than code comand will start it

Submaxillary answered 1/7, 2020 at 21:27 Comment(0)
W
0

For Windows you can use Command:

start Code filename.extension

The above line works for me.

Wernick answered 2/1, 2021 at 4:59 Comment(0)
T
0

Just update your python to python3.9 i did that and it worked for me.

Tree answered 17/4, 2022 at 11:26 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Willette

© 2022 - 2024 — McMap. All rights reserved.