210

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

4
  • 4
    On run prompt or command shell prompt just type - code -n "D:\myTextFile.txt" and get going.
    – RBT
    Commented Jun 8, 2017 at 1:37
  • 1
    code ./search.pl worked for me on windows 7 in visual studios own terminal
    – Coty Embry
    Commented Feb 16, 2018 at 15:44
  • 2
    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! Commented Jan 16, 2020 at 20:18
  • In case someone needs for Mac: youtube.com/watch?v=zWfNLB_CBFs
    – Om Sao
    Commented Sep 17, 2020 at 16:24

28 Answers 28

383

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.
7
  • 8
    Was looking for this to set vscode as diff-tool. You can use code -d file1 file2 to compare files.
    – BadAtLaTeX
    Commented Apr 13, 2017 at 14:48
  • 2
    I am on Windows and this most definitely does not work for me. Commented Dec 2, 2017 at 2:13
  • 4
    @ChristopherHarris if you're using the insiders build then it command is code-insiders
    – svict4
    Commented Feb 5, 2018 at 2:02
  • the docs explain everything: code.visualstudio.com/docs/editor/command-line
    – JP Lew
    Commented Apr 12, 2018 at 15:44
  • 1
    This command is straight up missing for me in VS Code 1.22.2 non-insiders. Commented Apr 18, 2018 at 8:05
85

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.

5
  • @NathanTuggy I think you forget to paste the window case.
    – GingerBear
    Commented Dec 9, 2015 at 22:49
  • 3
    Windows is automatically added when you install it. Commented Mar 8, 2016 at 5:39
  • 12
    Per the docs, you can now install it on Mac via the Command Palette (Cmd-Shift-P), Shell Command: Install 'code' in PATH Commented Mar 8, 2016 at 5:41
  • 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.)
    – joanwolk
    Commented Aug 24, 2017 at 10:17
  • Is there a reason you used $* instead of "${@}"? [ref] Commented Feb 20, 2023 at 8:24
32

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 .

If you are not sure about what is your path, type where code.cmd, and then, copy it without the \code.cmd after the ...\bin.

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.

3
  • 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
    – Kathir
    Commented May 23, 2019 at 4:13
  • 1
    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.
    – dgo
    Commented Mar 26, 2020 at 17:37
  • The Terminal should be restarted after step No. 6 so the new PATH values take effect.
    – Amr
    Commented Oct 23, 2020 at 15:16
22

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.

1
  • 1
    This is the right answer for mac and least hacky of all solutions
    – Cristian
    Commented May 15, 2017 at 13:43
8

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 .

7

Sometimes setting path from VS Code command palette does not work

Instead manually add your VS Code to your path:

  1. Run in terminal

    sudo nano /etc/paths

  2. Go to the bottom of the file, and enter the path you wish to add

  3. Hit control-x to quit. Enter “Y” to save the modified buffer.

  4. Restart your terminal and to test echo $PATH. You should something similar

~ echo $PATH /Users/shashank/.nvm/versions/node/v8.9.2/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Visual Studio Code.app/Contents/Resources/app/bin

Next time, you navigate to your project folder from terminal

Enter:

code .

or

code /path/to/project

Source

6

In the case of Linux and Mac, you want to navigate to the directory that you extracted the VSCode files using the 'cd' command. For example:

cd ~/Downloads/VSCode

Then you start the application by running..

./Code

'Code' being the name of the executable.

If you have root access on the machine, you can configure the system to allow you to start VSCode from anywhere by linking it to /usr/bin, where links to executables are often stored.

sudo ln -s /path/to/VSCode/folder/Code /usr/bin/Code    

You can now launch VSCode from anywhere by typing:

Code
4
  • In Mac it is actually a folder Visual Studo Code.app so, we cant make the ln -s Commented Apr 28, 2017 at 6:09
  • 2
    This is precisely how not to do it. The answers given below by Laily and others are correct.
    – Tom Morris
    Commented May 2, 2017 at 10:28
  • this does not work for mac. the process does not go to background and also gets permission issues. Commented Jul 24, 2017 at 12:36
  • this solution is help me in pop-os thank you very mush
    – Sidou Gmr
    Commented Nov 7, 2022 at 21:38
6

For command line heads you can also run

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

this will do the exact same thing as the Shell Command: Install 'code' command in PATH command feature in VSCode.

4

When installing on Windows, you will be prompted to add VS Code to your PATH.

I was trying to figure out how to open files with VS Code from the command line and I already had the capability - I just forgot I had already added it. You might already have it installed - check by navigating to a folder you want to open and running the command code . to open that folder.

4

In linux if you use code . it will open VS Code in the folder the terminal was in. Using code . Filename.cs it will open in folder and open said file.

3

For linux Debian the below can be done

$ export PATH=$PATH:/usr/share/code

Then run it

$ code
$ code file.py
$ code workingdir
2

Open command line and type:

cd your_folder_path
code.cmd . 

or

code.cmd your_folder_path

It will open your folder in Visual Studio Code. Make Sure, you are inside the correct folder after executing "cd your_folder_path" command.

2

On Windows you can add the following path to the system environment variables.

C:\Users\username\AppData\Local\Programs\Microsoft VS Code\bin
1

This works for Windows:

CMD> start vscode://file/o:/git/libzmq/builds/msvc/vs2017/libzmq.sln

But if the filepath has spaces, normally one would add double quotes around it, like this:

CMD> start "vscode://file/o:/git/lib zmq/builds/msvc/vs2017/libzmq.sln"

But this messes up with start, which can take a double-quoted title, so it will create a window with this name as the title and not open the project.

CMD> start "title" "vscode://file/o:/git/lib zmq/builds/msvc/vs2017/libzmq.sln"
1

typing "code" in dos command prompt worked for me

1

On Ubuntu the flatpak version seemed broken. I uninstalled it and downloaded the deb package right from Microsoft.

1

I use the following command to load a project quickly (in linux)

  1. cd into the project cd /project
  2. run command code pwd

similar steps can be used in other Os too.

1

In linux terminal you can just type:

$ code run

1

Windows:

Add code CLI path in a system environment variable.

in windows default code cli path is (username is you pc username)C:\Users\username\AppData\Local\Programs\Microsoft VS Code\bin

enter image description here

Then you can check it like this by taking your project folder and open new cmd and type code .

enter image description here

1
  • 1
    This is the correct answer if you're looking for Windows solution.
    – Sai
    Commented Oct 24, 2022 at 20:13
0

Step 1: create a .bat file with the name you want e.g vscode.bat Step 2: Write your path to Visual Studio Code Step 3: Save it in C:\Windows\System32 directory

**
C:
cd Users\Bino\AppData\Local\Programs\Microsoft VS Code
Code.exe**

Step 4: You can call visual studio code from any where by typing "vscode" which is the name of your bat file

0

This will work. This is your directory name "Directory_Name"

 sudo code --user-data-dir="Directory_Name"
0

Other easyway to do it on mac is :go to Command Palette[ Shift ⇧+ Command (⌘)+P] and type :Shell Command: Install 'code' command in PATH

once installed: Shell command 'code' successfully installed in PATH.

Then you can use code from the terminal as well.

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

0

A simple way is to go to your Project where you want to open it and type

code.cmd D:\PathTo\yourProject\MyProject

enter image description here

That's it. It will open your project in Visual Studio Code.

0

Delete old virtual environment and create a fresh virtual environment.

0

In a way I am reticent to add to the long list of answers. However, I searched this page for the word "portable" and came up empty. (And I did a full Stack Overflow search and also found nothing.) So I want to add this very specific answer for potential future searchers.

This answer is for if you installed VS Code in Portable Mode on Windows 10.

"Portable Mode" refers to what is described on the official VS Code web pages, which as of 21 January 2021 are found here: https://code.visualstudio.com. It does not mean the Visual Studio Code Portable project started/run by Gareth Flowers, or any similar project. (I am not saying anything bad about this or other projects - I have neither used nor evaluated.) If you are using one of those projects, you need to check with that project documentation/community - although this might work.

"Installing" VS Code in Portable Mode is downloading a .zip archive and extracting it locally wherever you want your VS Code "installation" to live. There is no actual installation or setup that is run, so there is no automatic adding of the code command to your PATH.


Answer

After extracting the Portable Mode VS Code files to the location of your choice, there should be a bin folder in that location. Copy the full path of that bin folder and add it to your System or User (your choice) PATH variable.

You should then be able to use the code command from PowerShell or CMD.

0

In linux you need to check first what is the name you your vscode binary file

When you get the binary file name check where it is by using this command : whereis your_file_name

Go to the / (root) and go to bin and rename file with any name what you want to call it. To rename : mv your_file_name your_changed_name Now you can access vscode from any where in terminal

This works on Debian based Os definitely

0

For Mac OS:

Inside Visual Studio Code press:

Command + Shift + P

Type: shell and select it:

enter image description here

Once you hit enter, you're all done.

enter image description here

Full article: https://www.freecodecamp.org/news/how-to-open-visual-studio-code-from-your-terminal/

Not the answer you're looking for? Browse other questions tagged or ask your own question.