LinuxCommandLibrary

code

Open Visual Studio Code editor

TLDR

Start Visual Studio Code

$ code
copy

Open specific files/directories
$ code [path/to/file_or_directory1 path/to/file_or_directory2 ...]
copy

Compare two specific files
$ code [[-d|--diff]] [path/to/file1] [path/to/file2]
copy

Open specific files/directories in a new window
$ code [[-n|--new-window]] [path/to/file_or_directory1 path/to/file_or_directory2 ...]
copy

Install/uninstall a specific extension
$ code --[install|uninstall]-extension [publisher.extension]
copy

Display diagnostic and process information about the running code window
$ code [[-s|--status]]
copy

Print installed extensions with their versions
$ code --list-extensions --show-versions
copy

Start the editor as a superuser (root) while storing user data in a specific directory
$ sudo code --user-data-dir [path/to/directory]
copy

SYNOPSIS

code [options] [paths...]
code [command] [args...]

PARAMETERS

-d, --diff
    Compare two files side-by-side in VS Code's diff editor.

-h, --help
    Display usage information and exit.

-n, --new-window
    Force opening a new VS Code window.

-r, --reuse-window
    Force opening in the last active VS Code window (this is the default behavior).

-w, --wait
    Wait for the files to be closed in VS Code before returning control to the terminal.

--add
    Add folder(s) to the last active VS Code window.

--goto :[:]
    Open a file at the specified line and character position.

--install-extension
    Install a VS Code extension by its unique ID.

--list-extensions
    List all installed VS Code extensions.

--profile
    Open VS Code with the specified profile.

--remote

:
    Open a path in a remote context (e.g., SSH, Dev Containers).

--version
    Print the VS Code version to the console.

DESCRIPTION

The code command is the command-line interface (CLI) for Visual Studio Code, a popular cross-platform source-code editor developed by Microsoft. It allows users to effortlessly open files, directories, or even remote workspaces directly from their terminal. This utility is instrumental in integrating VS Code into various development workflows, enabling quick file access, scripting, and version control operations. Whether you need to open a specific file for editing, start a new project in the current directory, or compare two files side-by-side, the code command provides a direct and efficient pathway to your editor. It's designed for seamless interaction, supporting numerous options to control how VS Code launches, such as opening in a new window, reusing an existing one, or waiting for files to be closed before returning control to the terminal.

CAVEATS

The code command requires Visual Studio Code to be installed on your system. On most operating systems, the command is added to your system's PATH during VS Code installation or via an explicit option within the editor (e.g., 'Shell Command: Install 'code' command in PATH' on macOS/Linux). Its functionality is entirely dependent on the installed VS Code application and its configuration.

INSTALLATION AND PATH INTEGRATION

While Visual Studio Code is installed like any other application, the code command typically requires an additional step to be accessible directly from the terminal. On macOS and Linux, VS Code prompts users to install the 'code' command into their system's PATH via the 'Shell Command: Install 'code' command in PATH' option available in the Command Palette (Ctrl+Shift+P or Cmd+Shift+P). This creates a symbolic link that makes the executable globally available.
On Windows, the installer usually offers an option to 'Add 'Open with Code' action to Windows Explorer file/directory context menu' and adds VS Code to the system PATH environment variable, making the code command available in PowerShell or Command Prompt automatically.

REMOTE DEVELOPMENT INTEGRATION

The code command plays a pivotal role in VS Code's Remote Development capabilities. With extensions like 'Remote - SSH', 'WSL', or 'Dev Containers', users can open remote folders or repositories directly using the code command. For example, code --remote ssh-remote+ /path/to/project can open a project residing on a remote SSH server. This seamless integration allows developers to leverage the full power of VS Code while their development environment resides remotely, bridging the gap between local and cloud-based development seamlessly.

HISTORY

The code command emerged with the initial public release of Visual Studio Code by Microsoft in April 2015. From its inception, the CLI was a core component, designed to offer a familiar and efficient way for developers to interact with the editor directly from their terminal, mirroring capabilities found in other popular editors like Sublime Text and Atom. Its evolution closely mirrors the development of VS Code itself, continuously gaining new features and options to support advancements like integrated terminal features, remote development capabilities, and profile management, solidifying its role as an indispensable tool in modern software development workflows.

SEE ALSO

vim(1), nano(1), subl(1) (Sublime Text CLI), atom(1) (Atom Editor CLI)

Copied to clipboard