How to call VS Code Editor from terminal / command line
Solution 1
To open a file or directory use the command:
code /path/to/file/or/directory/you/want/to/open
For macOS users, it needs to be installed manually:
- Launch VS Code.
-
Command + Shift + P
to open the Command Palette. - Type
shell command
, to find theShell Command: Install 'code' command in PATH
and select to install it. - Restart your terminal.
Solution 2
Per the docs:
Mac OS X
- Download Visual Studio Code for Mac OS X.
- Double-click on VSCode-osx.zip to expand the contents.
- Drag Visual Studio Code.app to the Applications folder, making it available in the Launchpad.
- Add VS Code to your Dock by right-clicking on the icon and choosing Options, Keep in Dock.
Tip: 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.
Tip: You can also add it to VS Code Insiders build by changing "com.microsoft.VSCodeInsiders". Also if you don't to type the whole word code, just change it to c.
Linux
- Download Visual Studio Code for Linux.
- Make a new folder and extract VSCode-linux-x64.zip inside that folder.
- Double click on Code to run Visual Studio Code.
Tip: If you want to run VS Code from the terminal, create the following link substituting /path/to/vscode/Code with the absolute path to the Code executable
sudo ln -s /path/to/vscode/Code /usr/local/bin/code
Now, you can simply type code . in any folder to start editing files in that folder.
Solution 3
VS Code is a must have code editor for 2018
For Windows 10 users a lot is possible, the same way the Mac OS users type code .
.
Look for you VS Code \bin folder path e.g C:\Program Files\Microsoft VS Code\bin
. The bin folder includes a file called code.cmd
Follow the steps below and be proud of the OS you use.
Search for "Advanced System Setting" from Start.
Click on Environment Variables
On System Variables choose "path" from Variable tab and click on Edit.
Click on New on the right side of the popup window.
Copy your path from the Explorer's breadcrumb path and paste it into the new opened path in step 4, example:-
C:\Program Files\Microsoft VS Code\bin
Click Ok on all the open windows to confirm changes and restart your
cmd
Go to your
cmd
and navigate to you working directory on server and typecode .
C:>cd wamp64\www\react-app> code .
to open with VS Code on Windows.
Visual Studio Code also includes a command prompt (terminal) window and you can open one or more of them with
Ctrl + `
on your keyboard.
Hope this helps some one like it did to many of us.
Solution 4
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.
Mac shell commands
Restart the terminal for the new $PATH
value to take effect. You'll be able to type code .
in any folder to start editing files in that folder.
Solution 5
For VS Code Insiders Windows users (vs code doc):
Add the directory "C:\Program Files (x86)\Microsoft VS Code Insiders\bin"
at %PATH% environmental variable.
then go to the folder that you want to open with vs code and type:
code-insders .
Related videos on Youtube
Levon
Updated on February 10, 2022Comments
-
Levon about 2 years
The question says it all.
How can I open VS Code editor from
- windows cmd
- linux and mac terminal
e.g. for notepad++ I write
> start notepad++ test.txt
By the way, the editor is awesome (cross-platform)! Thank you Nadella!
You can download it from microsoft
-
RBT almost 7 yearsOn run prompt or command shell prompt just type -
code -n "D:\myTextFile.txt"
and get going. -
Coty Embry about 6 years
code ./search.pl
worked for me on windows 7 in visual studios own terminal -
Neil Gaetano Lindberg over 4 years
code -n filename
-- Opens file in NEW window.code -r filename
-- Opens file in already opened window (this is what I wanted and why I'm commenting. It works great from the terminal window of VSCode).code -g filename
-- Handy! As you can see from runningcode --help
, the -g flag is short for--goto
and it is my favorite command to use when debugging. You can use just the file name and it acts just like-r
OR, you can use<file:line[:character]>
to go straight to a line, or even char on a line! -
Om Sao over 3 yearsIn case someone needs for Mac: youtube.com/watch?v=zWfNLB_CBFs
-
GingerBear over 8 years@NathanTuggy I think you forget to paste the window case.
-
Борис Чиликин about 8 yearsWindows is automatically added when you install it.
-
Борис Чиликин about 8 yearsPer the docs, you can now install it on Mac via the Command Palette (Cmd-Shift-P),
Shell Command: Install 'code' in PATH
-
gr4nt3d about 7 yearsWas looking for this to set vscode as diff-tool. You can use
code -d file1 file2
to compare files. -
Kishore Relangi almost 7 yearsIn Mac it is actually a folder
Visual Studo Code.app
so, we cant make the ln -s -
Tom Morris almost 7 yearsThis is precisely how not to do it. The answers given below by Laily and others are correct.
-
Cristian almost 7 yearsThis is the right answer for mac and least hacky of all solutions
-
Shamasis Bhattacharya almost 7 yearsthis does not work for mac. the process does not go to background and also gets permission issues.
-
joanwolk over 6 yearsOn Mac, this works, but will open a different instance of VS Code than the shortcut installed from the command palette. (I noticed I was running two versions simultaneously after adding and trying the above Mac shell code—and the filename search was much slower in the version opened by the manually installed shortcut.)
-
Anthony Gatlin over 6 yearsI am on Windows and this most definitely does not work for me.
-
svict4 about 6 years@ChristopherHarris if you're using the insiders build then it command is
code-insiders
-
JP Lew about 6 yearsthe docs explain everything: code.visualstudio.com/docs/editor/command-line
-
Tomáš Hübelbauer about 6 yearsThis command is straight up missing for me in VS Code 1.22.2 non-insiders.
-
Sahil Patel over 5 yearsI have tried this fix multiple times. It just goes vanishes when I restart the terminal, or maybe when I restart the computer.
-
Kathir almost 5 yearsThe files and folder structure has changed for the newer versions of VS Code (Windows). So,
code.cmd
was not available. BUT, thecode
command was added to PATH by default since installation -
dgo about 4 yearsFor anyone else - what @Kathir says above is not true for me. Was not in my path, and bin directory still contains
code.cmd
for me. I first tried to do this withcode.exe
, but it's kind of a pain, because by default, it dumps everything into the terminal and runs as an ongoing terminal process. -
Amr over 3 yearsThe Terminal should be restarted after step No. 6 so the new
PATH
values take effect.