77

Recently I downloaded visual studio code which operates as text editor but I can't figure out how to open code file from terminal.

When I use Sublime Text the command is subl, so when I run subl newFolder.c it would automatically open Sublime Text with a file named newFolder.c.

What can I type to create/open a file in Visual Studio Code?

David Foerster
  • 35,754
  • 55
  • 92
  • 145
Giorgi Cercvadze
  • 871
  • 1
  • 6
  • 3

2 Answers2

104

It should be called code and for me, I just have to run it.

$ code
$ code my-file

and that works. If installed the .deb (only available in older Ubuntu versions) the path should be

`/usr/bin/code`

If you installed it as a snap, e.g.

$ sudo snap install code --classic

Then it's path should be

`/snap/bin/code`

If you installed it another way, then try to find the binary, e.g.

command -v code

will print the path. command is a bit more reliable than which. VS Code is an Electron application (i.e. Chrome/node) and the launcher is a little wonky. For example, xdg-open does not work well for me.

NOTE ABOUT INSIDERS VERSION: As Nicholas Humphrey points out in the comments, it could also be called code-insiders if you installed the insiders version. This allows you to have side-by-side installations of regular and insider versions.

Michael Sandman
  • 1,731
  • 1
  • 10
  • 12
  • 4
    yeah!! worked for me. – Avnish Tiwary Jul 30 '17 at 08:08
  • 1
    "code ." will open current directory in vs code – Ali_Hr Feb 09 '20 at 06:55
  • 1
    Important notice: Only worked for me when I installed the debian package. It did NOT work when I installed the snap package - then I could open VS Code via GUI, but I don't know which command to use. – Martin Thoma Mar 06 '20 at 20:27
  • @MartinThoma I added some more info for `snap` installs. Also, see @DavidFoerster answer below. It should fix up the shell command if it's broken. – Michael Sandman Mar 08 '20 at 14:39
  • OMG! Been weeks looking for information and trying every single keyword and it is as easy as just `code`? Thank you! – Geppettvs D'Constanzo May 19 '20 at 16:42
  • none of these works for me, this is weird...really wish Linux could be as easy as Windows. In Windows it's just `code.` at any directory but in Linux it doesn't work. – Nicholas Humphrey May 27 '20 at 23:48
  • 1
    @NicholasHumphrey, sorry it's not going well for you. If you post your specific problem we might be able to help. – Michael Sandman May 30 '20 at 03:16
  • 2
    @MichaelSandman thanks, I figured it out a while ago actually. My version is the insider version so I need to call code insider instead of just code. Stupid me~~ – Nicholas Humphrey May 30 '20 at 19:55
  • 1
    For those people (like me) who installed VS Code through the GUI on Ubuntu, but don't know how to setup the command line option these two commands should work for you (Note the source below says use path `/usr/bin` instead, but mine was in `/snap/bin`): `sudo update-alternatives --set editor /snap/bin/code` and if that throws an error run this first then the other `sudo update-alternatives --install /usr/bin/editor editor $(which code) 10` Source: https://code.visualstudio.com/docs/setup/linux – casual234 Nov 11 '21 at 16:28
42

Correct way is to open Visual Studio Code and press Ctrl+Shift+P on Windows (or Cmd+Shift+P on Mac) then type Install 'code' command in PATH . At some point you should see an option come up that lets you install shell command, click it. Then open a new terminal window and type code.

Shane Hudson
  • 521
  • 4
  • 5