// Tutorial //

How To Install and Use the Visual Studio Code (VS Code) Command Line Interface

Published on November 30, 2021
Default avatar
By Brian Boucheron
Developer and author at DigitalOcean.
How To Install and Use the Visual Studio Code (VS Code) Command Line Interface

Introduction

Visual Studio Code is a free, open-source, and cross-platform text editor developed primarily by Microsoft. It uses web technologies such as JavaScript and CSS, which has helped facilitate a large ecosystem of community-created plugins to extend its functionality into many different programming languages and features.

In this tutorial, you’ll install the Visual Studio Code command line interface and learn how to use it to open files and directories, compare changes between files, and install extensions.

Prerequisites

To complete this tutorial, you’ll need to have Visual Studio Code installed. Please refer to the official Setting up Visual Studio Code documentation to find out how to install Code for your platform.

Installing the Visual Studio Code Command Line Interface

You may need to install the Visual Studio Code command line interface before using it. To do so, first launch the normal Visual Studio Code graphical interface. If this is your first time opening the app, the default screen will have a icon bar along the left, and a default welcome tab:

A screenshot of the default "Get Started" screen in the Visual Studio Code interface, including an icon toolbar along the left-hand side, a few suggested activities to get started such as "New File..." and "Open...", and some highlighted instructional walkthroughs

Visual Studio Code provides a built-in command to install its command line interface. Bring up Code’s Command Palette by typing Command+Shift+P on Mac, or Control+Shift+P on Windows and Linux:

A screenshot of the Visual Studio Code interface with the Command Palette activated, waiting for input to be entered after its '>' prompt

This will open a prompt near the top of your Code window. Type shell command into the prompt. It should autocomplete to the correct command which will read Shell Command: Install 'code' command in PATH:

A screenshot of the Visual Studio Code interface, with the Command Palette activated and the "Install 'code' command in PATH" command highlighted

Press ENTER to run the highlighted command. You may be prompted to enter your administrator credentials to finish the installation process.

You now have the code command line command installed.

Verify that the install was successful by running code with the --version flag:

  1. code --version
Output
1.62.1 f4af3cbf5a99787542e2a30fe1fd37cd644cc31f x64

If your output includes a version string, you’ve successfully installed the Visual Studio Code command line interface. The next few sections will show you a few ways to use it.

Opening Files with the code Command

Running the code command with one or more filenames will open those files in the Visual Studio Code GUI:

  1. code file1

This will open the file1 file in Code.

  1. code *.md

This will open all markdown (.md) files in the current directory in Code.

By default, the files will be opened in an existing Code window if one is available. Use the --new-window flag to force Visual Studio Code to open a new window for the specified files.

Opening a Directory with the code Command

Use the code command followed by one or more directory names to open the directories in a new Visual Studio Code window:

  1. code directory1 directory2

Code will open a new window for the directories. Use the --reuse-window flag to tell Code to reuse the existing frontmost window instead.

Opening a .code-workspace Workspace File with the code Command

Opening a workspace file with the code command works similar to opening directories:

  1. code example.code-workspace

This will open the example workspace in a new window, unless you reuse an existing window by adding the --reuse-window flag.

Installing an Extension Using the code Command

You can install Visual Studio Code extensions using the code command line tool as well. To do so, you’ll first need to know the extension’s unique identifier. To find this information, first navigate to the extension’s page on the Visual Studio Marketplace.

For instance, here is the page for the Jupyter Notebook extension:

https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter

Notice the itemName parameter in the address. This parameter’s value, ms-toolsai.jupyter, is this extension’s unique identifier.

You can also find this information on the Marketplace page itself, towards the bottom of the right-hand column in the More info section:

A screenshot of the Jupyter extension's page on the Visual Studio Marketplace, highlighting the 'Unique Identifier ms-toosai.jupyter' unique id information in the page's right-hand column

Once you have this unique id, you can use it with code --install-extension to install the extension:

  1. code --install-extension ms-toolsai.jupyter
Output
Installing extension 'ms-toolsai.jupyter'... Extension 'ms-toolsai.jupyter' v2021.11.1001489384 was successfully installed.

Use the same id with the --uninstall-extension flag to uninstall the extension.

Showing the Differences Between Two Files Using the code Command

To show a standard split-screen diff that will highlight the additions, deletions, and changes between two files, use the --diff flag:

  1. code --diff file1 file2

A screenshot of the Visual Studio Code diff interface, with two files side by side, and the second line highlighted, showing a few words have changed between the two versions

Similar to opening files, this will reuse the frontmost window by default, if one exists. To force a new window to open, use the --new-window flag.

Piping stdin Into Visual Studio Code Using the code Command

An important feature of most command line shells is the ability to pipe (or send) the output of one command to the input of the next. In the following command line, notice the | pipe character connecting the ls ~ command to code -:

  1. ls ~ | code -

This will execute the ls command on the ~ directory, which is a shortcut for the current user’s home directory. The output from ls will be a list of files and directories in your home directory. This will be sent to the code command, where the single - indicates that it should read the piped in text instead of a file.

code will output some information about the temporary file that it has created to hold the input:

Output
Reading from stdin via: /var/folders/dw/ncv0fr3x0xg7tg0c_cvfynvh0000gn/T/code-stdin-jfa

Then this file will open up in the Code GUI interface:

A screenshot of Visual Studio Code with a text file open, displaying the text piped in from the ls command. The text is standard directories such as Desktop and Documents, along with file1 and file2 used in the previous section

This command will continue to wait indefinitely for more input. Press CTRL+C to have code stop listening and return you to your shell.

Add the --new-window flag to force Code to open a new window for the input.

Conclusion

In this tutorial you installed Visual Studio Code’s code command line tool, and used it to open files and directories, compare files, and install extensions.

To learn more about the code command, you can run its --help function:

  1. code --help

You can also refer to the official Visual Studio Code command line documentation or take a look at our VS Code tag page for more Visual Studio Code tutorials, tech talks, and Q&A.

If you’ve enjoyed this tutorial and our broader community, consider checking out our DigitalOcean products which can also help you achieve your development goals.

Learn more here


About the authors
Default avatar
Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?
Leave a comment

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!