Reference to Visual Studio code variables (2023)

Visual Studio Code supports variable substitution inDebuggingandtaskConfiguration files and some selected settings. Variable substitution is supported in some key and value stringslaunch.jsonandTasks.jsonuse files${Variablenname}Syntax.

Predefined Variables

The following predefined variables are supported:

  • ${userHome}- the path of the user's home folder
  • ${workspaceFolder}- the path of the folder opened in VS Code
  • ${workspaceFolderBasename}- the name of the folder opened in VS Code without slashes (/)
  • ${file}- the currently open file
  • ${fileWorkspaceFolder}- Workspace folder of the currently open file
  • ${relativeDatei}- the currently opened file relative toworkspaceOrdner
  • ${relativeFileDirname}- the directory name of the currently open file relative toworkspaceOrdner
  • ${fileBasename}- the base name of the currently opened file
  • ${fileBasenameNoExtension}- the base name of the currently opened file without the file extension
  • ${fileExtname}- the extension of the currently opened file
  • ${fileDirname}- Folder path of the currently opened file
  • ${fileDirnameBasename}- Folder name of the currently opened file
  • ${cwd}- the current working directory of the task runner when starting VS Code
  • ${line number}- the currently selected line number in the active file
  • ${selected text}- the currently selected text in the active file
  • ${execPath}- the path to the running VS Code executable
  • ${defaultBuildTask}- the name of the default build task
  • ${pathSeparator}- the character used by the operating system to separate components in file paths

Examples of predefined variables

Suppose you have the following requirements:

  1. A file is located under/home/Ihr-Benutzername/Ihr-Projekt/Ordner/Datei.extopen in your editor;
  2. The directory/home/your-username/your-projectopened as your root workspace.

So you have the following values ​​for each variable:

  • ${userHome}-/home/your-username
  • ${workspaceFolder}-/home/your-username/your-project
  • ${workspaceFolderBasename}-your project
  • ${file}-/home/Ihr-Benutzername/Ihr-Projekt/Ordner/Datei.ext
  • ${fileWorkspaceFolder}-/home/your-username/your-project
  • ${relativeDatei}-folder/file.ext
  • ${relativeFileDirname}-Maps
  • ${fileBasename}-Data.ext
  • ${fileBasenameNoExtension}-file
  • ${fileDirname}-/home/your-username/your-project/folder
  • ${fileExtname}-.ext
  • ${line number}- Line number of the cursor
  • ${selected text}- text selected in your code editor
  • ${execPath}- Location of Code.exe
  • ${pathSeparator}-/on macOS or Linux,\on Windows

Top: Use intellisense inside string values ​​forTasks.jsonandlaunch.jsonto get a full list of predefined variables.

Variables that apply per workspace folder

By appending the root folder name to a variable (separated by a colon), it is possible to access sibling root folders of a workspace. Without the root folder name, the variable is restricted to the same folder in which it is used.

For example in a multi-root workspace with foldersServerandClient, a${workspaceFolder:Client}refers to the way ofClientRoot.

environment variables

You can also reference environment variables through the${env:Name}Syntax (z. B.${env:USERNAME}).

{ "Art":"Node", "Inquiry":"beginning", "Name":"Launcher", "Program":"${workspaceFolder}/app.js", "cwd":"${workspaceFolder}", "Arguments": ["${env:USERNAME}"]}

configuration variables

You can reference VS Code settings ("configurations") through${config:Name}Syntax (z. B.${config:editor.fontSize}).

command variables

If the predefined variables from above are not enough, you can use any VS Code command as a variable via${command:command id}Syntax.

A command variable is replaced by the (string) result of the command evaluation. The implementation of a command can range from a simple calculation with no UI to sophisticated functionality based on the UI features available through the VS Code Extension API. If the command returns anything other than a string, variable substitution will not complete. command variablesGot toreturn a string.

An example of this functionality is VS Code's Node.js debugger extension, which provides an interactive commandextension.pickNodeProcessto select a single process from the list of all running Node.js processes. The command returns the process ID of the selected process. This makes it possibleextension.pickNodeProcesscommand in oneAppend by process IDStart the configuration as follows:

{ "configurations": [{ "Art":"Node", "Inquiry":"attach", "Name":"Append by Process ID", "ProcessID":"${command:extension.pickNodeProcess}"}]}

When using a command variable in alaunch.jsonconfiguration, the enclosurelaunch.jsonThe configuration is passed to the command as an object via an argument. This allows commands to know the context and parameters of the specificlaunch.jsonConfiguration when invoked.

input variables

Command variables are already powerful, but they lack a mechanism to configure the command being run for a specific use case. For example, it is not possible to pass aprompt messageor adefault valueto a generic "user prompt".

This restriction is solved withinput variableswhich have the syntax:${input:variableID}. DievariableIDrefers to entries in theinputssection oflaunch.jsonandTasks.json, where additional configuration attributes are specified. Nesting of input variables is not supported.

The following example shows the overall structure of aTasks.jsonthat uses input variables:

{ "Execution":"2.0.0", "Tasks": [{ "Label":"task name", "Command":"${input:variableID}" // ...}], "Inputs": [{ "I would":"Variable ID", "Art":"Type of input variable" // Enter specific configuration attributes}]}

Currently VS Code supports three types of input variables:

  • promptString: Displays an input box to receive a string from the user.
  • pickString: Displays a quick-pick drop-down menu to allow the user to select from multiple options.
  • command: Runs any command.

Each type requires additional configuration attributes:

promptString:

  • designation: Appears in Quick Entry and provides context for typing.
  • Originally: Default value to be used if the user does not enter anything else.
  • password: Set to true to enter with a password prompt that does not display the entered value.

pickString:

  • designation: Appears in the quick selector and provides context for input.
  • options: A set of options for the user to choose from.
  • Originally: Default value to be used if the user does not enter anything else. It must be one of the option values.

An option can be a string value or an object with both a label and a value. The drop down menu will appearLabel: value.

command:

  • command: Command is executed with variable interpolation.
  • argument: Optional option pack passed to the implementation of the command.

Below is an example of aTasks.jsonthat illustrates the use ofinputsmit Angular CLI:

{ "Execution":"2.0.0", "Tasks": [{ "Label":"of g", "Art":"sleeve", "Command":"von", "Arguments": ["g","${input:componentType}","${input:componentname}"]}], "Inputs": [{ "Art":"pickString", "I would":"component type", "Designation":"What kind of component do you want to create?", "options": [ "Components", "policy", "Rohr", "Service", "Class", "guard", "Interface", "enum"], "Originally":"Components"},{ "Art":"promptString", "I would":"component name", "Designation":"Name your component.", "Originally":"my-new-component"}]}

Running the example:

Reference to Visual Studio code variables (1)

The following example shows how to use a user input variable of typecommandin a debug configuration that allows the user to select a test case from a list of all test cases found in a specific folder. An extension is assumed to provide oneextension.mochaSupport.testPickerCommand that finds all test cases in a configurable location and displays a selection UI to select one of them. The arguments to a command prompt are defined by the command itself.

{ "configurations": [{ "Art":"Node", "Inquiry":"beginning", "Name":"Run Special Test", "Program":"${workspaceFolder}/${input:pickTest}"}], "Inputs": [{ "I would":"pickTest", "Art":"Command", "Command":"extension.mochaSupport.testPicker", "Arguments": { "testOrdner":"/out/tests"}}]}

Command prompts can also be used with tasks. This example uses the built-in Terminate Task command. It can accept an argument to end all tasks.

{ "Execution":"2.0.0", "Tasks": [{ "Label":"End All Tasks", "Command":"echo ${input:terminate}", "Art":"sleeve", "problemMatcher": []}], "Inputs": [{ "I would":"break up", "Art":"Command", "Command":"workbench.action.tasks.terminate", "Arguments":"terminateAll"}]}

Frequently Asked Questions

Details of variable substitution in a debug configuration or task

Variable substitution in debug configurations or tasks is a two-pass process:

  • In the first pass, all variables are evaluated to string results. If a variable occurs more than once, it is only evaluated once.
  • In the second run, all variables are replaced with the results from the first run.

A consequence of this is that the evaluation of a variable (e.g. a command-based variable implemented in an extension) occursno accessto other substituted variables in the debug configuration or task. It only sees the original variables. This means that the variables cannot depend on each other (which ensures isolation and makes the substitution robust to the order of evaluation).

Is variable substitution supported in user and workspace settings?

The predefined variables are supported in a selected number of setting buttonssettings.jsonFiles like the terminalcwd,env,sleeveandSchaleArgsValues. Somethe settingslikeWindow.Titlehave their own variables:

 "Window.Title":"${dirty}${activeEditorShort}${separator}${rootName}${separator}${appName}"

Note the comments in the settings editor (⌘,(Windows, Linuxctrl+,)) to learn more about setting specific variables.

Why isn't ${workspaceRoot} documented?

Die Variable${workspaceRoot}was discarded in favor of${workspaceFolder}better alignMulti-root workspaceSupport.

Why are variables not resolved in tasks.json?

Not all values ​​includedTasks.jsonSupport variable substitution. Namely onlycommand,argument, andoptionsSupport variable substitution. input variables in theinputsSection is not resolved because nesting of input variables is not supported.

How can I find the actual value of a variable?

An easy way to check the runtime value of a variable is to create a VS Codetaskto print the variable value to the console. For example, to show the resolved value for${workspaceFolder}, you can build and run (Terminal>perform task) the following simple 'echo' task inTasks.json:

{ "Execution":"2.0.0", "Tasks": [{ "Label":"Echo", "Art":"sleeve", "Command":"echo ${workspaceFolder}"}]}

7.12.2022

Top Articles
Latest Posts
Article information

Author: Velia Krajcik

Last Updated: 04/10/2023

Views: 5786

Rating: 4.3 / 5 (54 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Velia Krajcik

Birthday: 1996-07-27

Address: 520 Balistreri Mount, South Armand, OR 60528

Phone: +466880739437

Job: Future Retail Associate

Hobby: Polo, Scouting, Worldbuilding, Cosplaying, Photography, Rowing, Nordic skating

Introduction: My name is Velia Krajcik, I am a handsome, clean, lucky, gleaming, magnificent, proud, glorious person who loves writing and wants to share my knowledge and understanding with you.