Skip to content

Commit 85af33f

Browse files
committed
Move variables article to Reference ToC node
1 parent 8bc2e69 commit 85af33f

File tree

3 files changed

+82
-69
lines changed

3 files changed

+82
-69
lines changed
Lines changed: 3 additions & 0 deletions
Loading

docs/editor/variables-reference.md renamed to docs/reference/variables-reference.md

Lines changed: 76 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -7,63 +7,66 @@ PageTitle: Visual Studio Code Variables Reference
77
DateApproved: 02/06/2025
88
MetaDescription: Visual Studio Code variable substitution reference
99
---
10-
# Variables Reference
10+
# Variables reference
1111

12-
Visual Studio Code supports variable substitution in [Debugging](/docs/editor/debugging.md) and [Task](/docs/editor/tasks.md) configuration files as well as some select settings. Variable substitution is supported inside some key and value strings in `launch.json` and `tasks.json` files using **${variableName}** syntax.
12+
Visual Studio Code supports variable substitution in [Debugging](/docs/editor/debugging.md) and [Task](/docs/editor/tasks.md) configuration files, and for some select settings. Variable substitution is supported inside some key and value strings in `launch.json` and `tasks.json` files by using the **${variableName}** syntax.
1313

1414
## Predefined variables
1515

1616
The following predefined variables are supported:
1717

18-
- **${userHome}** - the path of the user's home folder
19-
- **${workspaceFolder}** - the path of the folder opened in VS Code
20-
- **${workspaceFolderBasename}** - the name of the folder opened in VS Code without any slashes (/)
21-
- **${file}** - the current opened file
22-
- **${fileWorkspaceFolder}** - the current opened file's workspace folder
23-
- **${relativeFile}** - the current opened file relative to `workspaceFolder`
24-
- **${relativeFileDirname}** - the current opened file's dirname relative to `workspaceFolder`
25-
- **${fileBasename}** - the current opened file's basename
26-
- **${fileBasenameNoExtension}** - the current opened file's basename with no file extension
27-
- **${fileExtname}** - the current opened file's extension
28-
- **${fileDirname}** - the current opened file's folder path
29-
- **${fileDirnameBasename}** - the current opened file's folder name
30-
- **${cwd}** - the task runner's current working directory upon the startup of VS Code
31-
- **${lineNumber}** - the current selected line number in the active file
32-
- **${columnNumber}** - the current selected column number in the active file
33-
- **${selectedText}** - the current selected text in the active file
34-
- **${execPath}** - the path to the running VS Code executable
35-
- **${defaultBuildTask}** - the name of the default build task
36-
- **${pathSeparator}** - the character used by the operating system to separate components in file paths
37-
- **${/}** - shorthand for **${pathSeparator}**
38-
39-
### Predefined variables examples
40-
41-
Supposing that you have the following requirements:
18+
| Variable | Description |
19+
|--------------------------------|-----------------------------------------------------------------------------|
20+
| **${userHome}** | Path of the user's home folder |
21+
| **${workspaceFolder}** | Path of the folder opened in VS Code |
22+
| **${workspaceFolderBasename}** | Name of the folder opened in VS Code without any slashes (/) |
23+
| **${file}** | Currently opened file |
24+
| **${fileWorkspaceFolder}** | Currently opened file's workspace folder |
25+
| **${relativeFile}** | Currently opened file relative to `workspaceFolder` |
26+
| **${relativeFileDirname}** | Currently opened file's dirname relative to `workspaceFolder` |
27+
| **${fileBasename}** | Currently opened file's basename |
28+
| **${fileBasenameNoExtension}** | Currently opened file's basename with no file extension |
29+
| **${fileExtname}** | Currently opened file's extension |
30+
| **${fileDirname}** | Currently opened file's folder path |
31+
| **${fileDirnameBasename}** | Currently opened file's folder name |
32+
| **${cwd}** | Task runner's current working directory upon the startup of VS Code |
33+
| **${lineNumber}** | Currently selected line number in the active file |
34+
| **${columnNumber}** | Currently selected column number in the active file |
35+
| **${selectedText}** | Currently selected text in the active file |
36+
| **${execPath}** | Path to the running VS Code executable |
37+
| **${defaultBuildTask}** | Name of the default build task |
38+
| **${pathSeparator}** | Character used by the operating system to separate components in file paths |
39+
| **${/}** | Shorthand for **${pathSeparator}** |
40+
41+
### Predefined variables example
42+
43+
Suppose that you have the following conditions:
4244

4345
1. A file located at `/home/your-username/your-project/folder/file.ext` opened in your editor;
44-
2. The directory `/home/your-username/your-project` opened as your root workspace.
45-
46-
So you will have the following values for each variable:
47-
48-
- **${userHome}** - `/home/your-username`
49-
- **${workspaceFolder}** - `/home/your-username/your-project`
50-
- **${workspaceFolderBasename}** - `your-project`
51-
- **${file}** - `/home/your-username/your-project/folder/file.ext`
52-
- **${fileWorkspaceFolder}** - `/home/your-username/your-project`
53-
- **${relativeFile}** - `folder/file.ext`
54-
- **${relativeFileDirname}** - `folder`
55-
- **${fileBasename}** - `file.ext`
56-
- **${fileBasenameNoExtension}** - `file`
57-
- **${fileExtname}** - `.ext`
58-
- **${fileDirname}** - `/home/your-username/your-project/folder`
59-
- **${fileDirnameBasename}** - `folder`
60-
- **${lineNumber}** - line number of the cursor
61-
- **${columnNumber}** - column number of the cursor
62-
- **${selectedText}** - text selected in your code editor
63-
- **${execPath}** - location of Code.exe
64-
- **${pathSeparator}** - `/` on macOS or linux, `\` on Windows
65-
66-
>**Tip**: Use IntelliSense inside string values for `tasks.json` and `launch.json` to get a full list of predefined variables.
46+
1. The directory `/home/your-username/your-project` opened as your root workspace.
47+
48+
This leads to the following values for each of the variables:
49+
50+
- **${userHome}**: `/home/your-username`
51+
- **${workspaceFolder}**: `/home/your-username/your-project`
52+
- **${workspaceFolderBasename}**: `your-project`
53+
- **${file}**: `/home/your-username/your-project/folder/file.ext`
54+
- **${fileWorkspaceFolder}**: `/home/your-username/your-project`
55+
- **${relativeFile}**: `folder/file.ext`
56+
- **${relativeFileDirname}**: `folder`
57+
- **${fileBasename}**: `file.ext`
58+
- **${fileBasenameNoExtension}**: `file`
59+
- **${fileExtname}**: `.ext`
60+
- **${fileDirname}**: `/home/your-username/your-project/folder`
61+
- **${fileDirnameBasename}**: `folder`
62+
- **${lineNumber}**: line number of the cursor
63+
- **${columnNumber}**: column number of the cursor
64+
- **${selectedText}**: text selected in your code editor
65+
- **${execPath}**: location of Code.exe
66+
- **${pathSeparator}**: `/` on macOS or linux, `\` on Windows
67+
68+
> [!TIP]
69+
> Use IntelliSense inside string values for `tasks.json` and `launch.json` to get a full list of predefined variables.
6770
6871
### Variables scoped per workspace folder
6972

@@ -73,7 +76,7 @@ For example, in a multi root workspace with folders `Server` and `Client`, a `${
7376

7477
## Environment variables
7578

76-
You can also reference environment variables through the **${env:Name}** syntax (for example, `${env:USERNAME}`).
79+
You can reference environment variables with the **${env:Name}** syntax. For example, **${env:USERNAME}** references the `USERNAME` environment variable.
7780

7881
```json
7982
{
@@ -88,11 +91,11 @@ You can also reference environment variables through the **${env:Name}** syntax
8891

8992
## Configuration variables
9093

91-
You can reference VS Code settings ("configurations") through **${config:Name}** syntax (for example, `${config:editor.fontSize}`).
94+
To reference VS Code settings (_configurations_), use the **${config:Name}** syntax. For example, **${config:editor.fontSize}** references the `editor.fontSize` setting.
9295

9396
## Command variables
9497

95-
If the predefined variables from above are not sufficient, you can use any VS Code command as a variable through the **${command:commandID}** syntax.
98+
You can use any VS Code command as a variable with the **${command:commandID}** syntax.
9699

97100
A command variable is replaced with the (string) result from the command evaluation. The implementation of a command can range from a simple calculation with no UI, to some sophisticated functionality based on the UI features available via VS Code's extension API. If the command returns anything other than a string, then the variable replacement will not complete. Command variables **must** return a string.
98101

@@ -111,13 +114,13 @@ An example of this functionality is in VS Code's Node.js debugger extension, whi
111114
}
112115
```
113116

114-
When using a command variable in a `launch.json` configuration, the enclosing `launch.json` configuration is passed as an object to the command via an argument. This allows commands to know the context and parameters of the specific `launch.json` configuration when they are called.
117+
When using a command variable in a `launch.json` configuration, the enclosing `launch.json` configuration is passed as an object to the command via an argument. This enables commands to know the context and parameters of the specific `launch.json` configuration when they are called.
115118

116119
## Input variables
117120

118121
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 a **prompt message** or a **default value** to a generic "user input prompt".
119122

120-
This limitation is solved with **input variables** which have the syntax: `${input:variableID}`. The `variableID` refers to entries in the `inputs` section of `launch.json` and `tasks.json`, where additional configuration attributes are specified. Nesting of input variables is not supported.
123+
This limitation is solved with **input variables**, which have the syntax **${input:variableID}**. The `variableID` refers to entries in the `inputs` section of `launch.json` and `tasks.json`, where additional configuration attributes are specified. Nesting of input variables is not supported.
121124

122125
The following example shows the overall structure of a `tasks.json` that makes use of input variables:
123126

@@ -143,30 +146,30 @@ The following example shows the overall structure of a `tasks.json` that makes u
143146

144147
Currently VS Code supports three types of input variables:
145148

146-
- **promptString**: Shows an input box to get a string from the user.
147-
- **pickString**: Shows a Quick Pick dropdown to let the user select from several options.
148-
- **command**: Runs an arbitrary command.
149+
- **promptString**: shows an input box to get a string from the user.
150+
- **pickString**: shows a Quick Pick dropdown to let the user select from several options.
151+
- **command**: runs an arbitrary command.
149152

150153
Each type requires additional configuration attributes:
151154

152155
`promptString`:
153156

154-
- **description**: Shown in the quick input, provides context for the input.
155-
- **default**: Default value that will be used if the user doesn't enter something else.
156-
- **password**: Set to true to input with a password prompt that will not show the typed value.
157+
- **description**: shown in the quick input, provides context for the input.
158+
- **default**: default value that will be used if the user doesn't enter something else.
159+
- **password**: set to true to input with a password prompt that will not show the typed value.
157160

158161
`pickString`:
159162

160-
- **description**: Shown in the quick pick, provides context for the input.
161-
- **options**: An array of options for the user to pick from.
162-
- **default**: Default value that will be used if the user doesn't enter something else. It must be one of the option values.
163+
- **description**: shown in the quick pick, provides context for the input.
164+
- **options**: an array of options for the user to pick from.
165+
- **default**: default value that will be used if the user doesn't enter something else. It must be one of the option values.
163166

164167
An option can be a string value or an object with both a label and value. The dropdown will display **label: value**.
165168

166169
`command`:
167170

168-
- **command**: Command being run on variable interpolation.
169-
- **args**: Optional option bag passed to the command's implementation.
171+
- **command**: runs a command on variable interpolation.
172+
- **args**: optional option bag passed to the command's implementation.
170173

171174
Below is an example of a `tasks.json` that illustrates the use of `inputs` using Angular CLI:
172175

@@ -205,7 +208,7 @@ Below is an example of a `tasks.json` that illustrates the use of `inputs` using
205208

206209
Running the example:
207210

208-
![Inputs Example](images/tasks/run-input-example.gif)
211+
![Inputs Example](images/variables-reference/run-input-example.gif)
209212

210213
The following example shows how to use a user input variable of type `command` in a debug configuration that lets the user pick a test case from a list of all test cases found in a specific folder. It is assumed that some extension provides an `extension.mochaSupport.testPicker` command that locates all test cases in a configurable location and shows a picker UI to pick one of them. The arguments for a command input are defined by the command itself.
211214

@@ -269,13 +272,13 @@ A consequence of this is that the evaluation of a variable (for example, a comma
269272

270273
### Is variable substitution supported in User and Workspace settings?
271274

272-
The predefined variables are supported in a select number of setting keys in `settings.json` files such as the terminal `cwd`, `env`, `shell` and `shellArgs` values. Some [settings](/docs/editor/settings.md) like `setting(window.title)` have their own variables:
275+
The predefined variables are supported in a select number of setting keys in `settings.json` files such as the terminal `cwd`, `env`, `shell` and `shellArgs` values. Some settings like `setting(window.title)` have their own variables:
273276

274277
```json
275278
"window.title": "${dirty}${activeEditorShort}${separator}${rootName}${separator}${appName}"
276279
```
277280

278-
Refer to the comments in the Settings editor (`kb(workbench.action.openSettings)`) to learn about setting specific variables.
281+
Refer to the comments in the Settings editor (`kb(workbench.action.openSettings)`) to learn about setting-specific variables.
279282

280283
### Why isn't ${workspaceRoot} documented?
281284

@@ -300,3 +303,8 @@ One easy way to check a variable's runtime value is to create a VS Code [task](/
300303
}
301304
]
302305
}
306+
307+
## Related resources
308+
309+
- [Debugging](/docs/editor/debugging.md)
310+
- [Tasks](/docs/editor/tasks.md)

docs/toc.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,9 @@
346346
"area": "reference",
347347
"topics": [
348348
["Default Keyboard Shortcuts", "/docs/reference/default-keybindings"],
349-
["Default Settings", "/docs/reference/default-settings"]
349+
["Default Settings", "/docs/reference/default-settings"],
350+
["Substitution Variables", "/docs/reference/variables-reference"]
351+
350352
]
351353
}
352354
]

0 commit comments

Comments
 (0)