How to call VS Code Editor from terminal / command line

232,432

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:

  1. Launch VS Code.
  2. Command + Shift + P to open the Command Palette.
  3. Type shell command, to find the Shell Command: Install 'code' command in PATH and select to install it.
  4. Restart your terminal.

Solution 2

Per the docs:

Mac OS X

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

  1. Download Visual Studio Code for Linux.
  2. Make a new folder and extract VSCode-linux-x64.zip inside that folder.
  3. 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.

  1. Search for "Advanced System Setting" from Start.

  2. Click on Environment Variables

  3. On System Variables choose "path" from Variable tab and click on Edit.

  4. Click on New on the right side of the popup window.

  5. 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

  6. Click Ok on all the open windows to confirm changes and restart your cmd

  7. Go to your cmd and navigate to you working directory on server and type code .

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 .

Share:
232,432

Related videos on Youtube

Levon
Author by

Levon

Updated on February 10, 2022

Comments

  • Levon
    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
      RBT almost 7 years
      On run prompt or command shell prompt just type - code -n "D:\myTextFile.txt" and get going.
    • Coty Embry
      Coty Embry about 6 years
      code ./search.pl worked for me on windows 7 in visual studios own terminal
    • Neil Gaetano Lindberg
      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 running code --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
      Om Sao over 3 years
      In case someone needs for Mac: youtube.com/watch?v=zWfNLB_CBFs
  • GingerBear
    GingerBear over 8 years
    @NathanTuggy I think you forget to paste the window case.
  • Борис Чиликин
    Борис Чиликин about 8 years
    Windows is automatically added when you install it.
  • Борис Чиликин
    Борис Чиликин about 8 years
    Per the docs, you can now install it on Mac via the Command Palette (Cmd-Shift-P), Shell Command: Install 'code' in PATH
  • gr4nt3d
    gr4nt3d about 7 years
    Was looking for this to set vscode as diff-tool. You can use code -d file1 file2 to compare files.
  • Kishore Relangi
    Kishore Relangi almost 7 years
    In Mac it is actually a folder Visual Studo Code.app so, we cant make the ln -s
  • Tom Morris
    Tom Morris almost 7 years
    This is precisely how not to do it. The answers given below by Laily and others are correct.
  • Cristian
    Cristian almost 7 years
    This is the right answer for mac and least hacky of all solutions
  • Shamasis Bhattacharya
    Shamasis Bhattacharya almost 7 years
    this does not work for mac. the process does not go to background and also gets permission issues.
  • joanwolk
    joanwolk over 6 years
    On 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
    Anthony Gatlin over 6 years
    I am on Windows and this most definitely does not work for me.
  • svict4
    svict4 about 6 years
    @ChristopherHarris if you're using the insiders build then it command is code-insiders
  • JP Lew
    JP Lew about 6 years
    the docs explain everything: code.visualstudio.com/docs/editor/command-line
  • Tomáš Hübelbauer
    Tomáš Hübelbauer about 6 years
    This command is straight up missing for me in VS Code 1.22.2 non-insiders.
  • Sahil Patel
    Sahil Patel over 5 years
    I have tried this fix multiple times. It just goes vanishes when I restart the terminal, or maybe when I restart the computer.
  • Kathir
    Kathir almost 5 years
    The files and folder structure has changed for the newer versions of VS Code (Windows). So, code.cmd was not available. BUT, the code command was added to PATH by default since installation
  • dgo
    dgo about 4 years
    For 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 with code.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
    Amr over 3 years
    The Terminal should be restarted after step No. 6 so the new PATH values take effect.