Manage VS Code from Command-Line Interface like a Pro

Microsoft Visual Studio Code provides a powerful command-line interface for installing extensions, opening files, changing the display language, and diagnostics reports.

This tool is preinstalled at the time of VS Code installation on your system and can be operated using the code command.

Opening Files and Directories

You can easily open files, directories, or projects with the help of a code command within the context of a folder. To open a file from a code command. First, open your terminal app, navigate to the project directory, and execute the code ./filename command.

Note: If the file does not exist, VS Code will create a new file with the specified name. Also, if you specify more than one file, VS Code will only open the files in separate tabs.

$ cd mydir/
$ code file.txt

Below is the output of the above command.

To open a directory within the context of a folder. Navigate to the project directory from the terminal app and execute the code . command.

Note: If you specify more than one directory, VS Code will create a Multi-root Workspace including each folder in context area.

$ cd mydir/
$ code .

Below is the output of the above command.

Managing Extensions

The code command provides you with options to list, install, remove, disable, etc., for the extensions. For that, you need to know the extension with the publisher name to use publisher.extension as an argument.

Listing the Installed Extension

Let’s start by listing the installed extension in VS Code using the below command.

$ code --list-extensions

The output will include enabled or disabled extensions, as shown below.

Listing enabled extensions using code command
Listing enabled extensions using code command

Use the --show-versions option to output the extension with its version, as shown below.

$ code --list-extensions --show-versions

Below is the output of the above command.

Listing enabled extensions with their versions using code command
Listing enabled extensions with their versions using code command

Installing Extension

To install the extension from the code command, you need to know the extension and its publisher name to provide the full extension name publisher.extension as an argument.

First, visit the Visual Studio Code Marketplace and search for an extension to install, as shown below.

Search for your extension on Visual Studio Code Marketplace
Search for your extension on Visual Studio Code Marketplace

As shown below, click on the extension name that you want to install from the VS Marketplace.

Click to your extension on Visual Studio Code Marketplace
Click to your extension on Visual Studio Code Marketplace

Take note of ItemName from the URL section, as shown below.

Take Note of ItemName on Visual Studio Code Marketplace
Take Note of ItemName on Visual Studio Code Marketplace

The ItemName includes the publisher and extension name in publisher.extension format. Use it as an argument to install the extension using the code command, as shown below.

$ code --install-extension HookyQR.beautify

Below is the output of the above command.

Installing extension from the code command
Installing extension from the code command

If you want to download the extension and install it later, as shown below, switch to the Version History tab and download the latest version.

Downloading extension from Visual Studio Code Marketplace
Downloading extension from Visual Studio Code Marketplace

After the file is successfully downloaded, specify the absolute path of the extension with the code --install-extension command, as shown below.

$ code --install-extension /path/to/the/HookyQR.beautify-1.5.0.vsix

Below is the output of the above command.

Installing downloaded extension from the code command
Installing the downloaded extension from the code command

You can re-execute code --list-extensions to verify the installation is successfully installed.

$ code --list-extensions

Below is the output of the above command.

Verify the extension installation
Verify the extension installation

Disable Extension

You can easily disable the installed extension using the code command. It will still be visible in the Disabled section of the extension view but will never be activated without manual action.

Note: Enabling option from command-line is not available for disabled extensions, but you can use a VS Code GUI application to enable your disabled extensions.

To disable the extension, specify the extension with the publisher name in publisher.extension format with the --disable-extension option, as shown below.

$ code --disable-extension HookyQR.beautify

Below is the output of the above command.

Disable enabled extension using code command

To disable all extensions pass the option without argument, as shown below.

$ code --disable-extensions

Below is the output of the above command.

Disable enabled extension using code command
Disable enabled extension using code command

It will be helpful while diagnosing the cause of VS Code failure. If disabling all extensions will solve the issue, it means any specific or multiple extensions are causing the problem.

Uninstall Extension

If the installed extensions are no longer required, you can free the space by removing them. To uninstall the extension, specify the extension with the publisher name in publisher.extension format with the --uninstall-extensions option as shown below.

$ code --uninstall-extension HookyQR.beautify

Below is the output of the above command.

Uninstall extension using code command
Uninstall extension using code command

Changing the Display Language

Visual Studio Code ships by default with English as the display language, but other languages are available through Marketplace Language Packs.

Below is the list of accepted language codes.

Display LanguageLocaleMarketplace
English (US)enInstalled
Simplified Chinesezh-cnLink
Traditional Chinesezh-twLink
FrenchfrLink
GermandeLink
ItalianitLink
SpanishesLink
JapanesejaLink
KoreankoLink
RussianruLink
Portuguese (Brazil)pt-brLink
TurkishtrLink
PolishplLink
CzechcsLink

Download the language pack you prefer to switch to and install it using the same method followed in the extension installation.

After that, specify the language code with the --locale option as shown below.

$ code --locale ja

Below is the output of the above command.

Changing the display language
Changing the display language

Help

$ code --help

Below is the output of the above command.

Visual Studio Code Help Section

FAQ

1) ‘code’ is not recognized as an internal or external command.

Your OS cannot find the VS Code binary code on its path. The VS Code Windows and Linux installations should have installed VS Code on your path. Try uninstalling and reinstalling VS Code.
On macOS, you need to manually run the Shell Command: Install ‘code’ command in PATH command (available through the Command Palette, Ctrl+Shift+P).

2) How do I get access to a command line (terminal) from within VS Code?

VS Code has an integrated terminal where you can run command-line tools from within VS Code.

3) How to open an Integrated Terminal?

a. Use the Ctrl+` keyboard shortcut with the backtick character.
b. Use the View > Terminal menu command.
c. From the Command Palette (Ctrl+Shift+P), use the View: Toggle Terminal command.
d. You can create a new terminal via the Terminal menu by selecting Terminal > New Terminal.

4) Can I specify the location of the settings for VS Code in order to have a portable version?

Not directly through the command line, but VS Code has a Portable Mode feature that lets you keep settings and data in the same location as your installation, for example, on a USB drive.

If you have more queries regarding this topic, do let us know in the comments section.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments