How to Use VS Code Variables in Settings, Tasks, and Debug Configurations

Use VS Code variables like ${workspaceFolder}, ${file}, and ${env:NAME} to write portable settings, tasks, and launch configurations that work across machines and operating systems.

6 min read

VS Code variables replace machine-specific values when a task or debug configuration runs. Instead of hardcoding a project path, use the workspaceFolder variable in a supported field and let VS Code resolve the open folder.

Variables work in tasks.json, launch.json, and selected settings, but not in every string field. A variable name goes inside dollar-sign braces, as shown in the examples below. Each configuration type decides which fields support it. See the official variables reference for the current list.

The most useful predefined variables

These variables are built into VS Code and work without any configuration:

VariableResolves toUse case
workspaceFolderPath of the open folderPaths to project files
workspaceFolderBasenameOpen folder name without slashesMessages or command arguments
fileFull path of the active fileRunning a tool on the current file
relativeFileActive file path relative to the workspacePortable tool arguments
fileBasenameActive file name with extensionOutput names or arguments
fileBasenameNoExtensionActive file name without extensionOutput names or arguments
fileDirnameFolder containing the active fileWorking directories
selectedTextText selected in the active editorPassing a selection to a tool
userHomeUser's home directoryUser-level paths
execPathPath to the running VS Code executableTools that need the editor path

Use IntelliSense inside a supported string value in tasks.json or launch.json to see the current predefined variables and descriptions.

Use variables in launch.json

The following complete launch.json needs Node.js and VS Code's built-in JavaScript debugger. It starts the active JavaScript file instead of storing an absolute program path.

jsonjson
{
  "version": "0.2.0",
  "configurations": [{
    "type": "node",
    "request": "launch",
    "name": "Launch current file",
    "program": "${file}"
  }]
}

Open a JavaScript file, choose Launch current file in Run and Debug, and start debugging. A debug session for the active file confirms that the variable resolved.

Use variables in tasks.json

Tasks support substitution only in the command, args, and options fields. This harmless echo task prints the resolved workspace path.

jsonjson
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Show workspace path",
      "type": "shell",
      "command": "echo",
      "args": ["${workspaceFolder}"]
    }
  ]
}

Run Show workspace path from Terminal > Run Task. The terminal should print the open folder's absolute path.

Environment variables

Use the env:NAME form to reference an environment variable that already exists in the VS Code process environment. The example below uses USERNAME, but you should substitute a variable that exists in the environment used to start VS Code.

jsonjson
{
  "type": "node",
  "request": "launch",
  "name": "Launch with env var",
  "program": "${workspaceFolder}/app.js",
  "args": ["${env:USERNAME}"]
}

This configuration object passes USERNAME as a normal program argument when that environment variable exists in the VS Code process. Environment variable names are platform-specific, so verify the name in your environment instead of assuming one name works everywhere.

Do not pass secrets through command arguments because they can appear in logs, terminal history, or process inspection. Use the runtime's supported secret or environment mechanism instead.

Configuration variables

Use the config:settingId form to reference a VS Code setting. For example, config:editor.fontSize reads the current font-size setting.

The destination field must support substitution and accept the resolved value. Configuration variables do not make an unsupported task or setting field substitutable.

Multi-root workspace variables

In a multi-root workspace, append a root name after a colon. workspaceFolder:Client resolves to the path of the root displayed as Client.

This is essential when you have separate frontend and backend folders and need to reference files across them:

jsonjson
{
  "type": "node",
  "request": "launch",
  "name": "Launch server with client path",
  "program": "${workspaceFolder:Server}/app.js",
  "args": ["${workspaceFolder:Client}/dist"]
}

Input variables

Input variables obtain a value when a task or debug session starts. Define them in an inputs section in launch.json or tasks.json, then reference the ID from a supported field.

Here is a task that asks which component to generate:

jsonjson
{
  "version": "2.0.0",
  "tasks": [
    { "label": "Preview component name", "type": "shell", "command": "echo", "args": ["Component: ${input:componentName}"] }
  ],
  "inputs": [
    { "id": "componentName", "type": "promptString", "description": "Component name", "default": "my-component" }
  ]
}

When you run Preview component name, VS Code asks for a name and then prints it. The input is an argument to a harmless command, not the command executable itself.

Three input types are available:

TypeWhat it does
promptStringShows a text input box; it can optionally mask password input
pickStringShows a dropdown containing configured options
commandRuns a VS Code command and uses its string return value

Masking a promptString hides what the user types in the prompt, but it does not guarantee that the substituted value stays out of logs or process arguments. Input variables cannot be nested.

A command input can invoke built-in or extension-provided behavior, so review the command ID and its effects before running the task or debug configuration.

Platform portability

On Windows, file paths use backslashes. On macOS and Linux, they use forward slashes. Hardcoded separators can make a composed path platform-specific.

Predefined path variables resolve using the current platform. When you construct a path from separate components, use pathSeparator or its slash shorthand:

jsonjson
{
  "command": "node",
  "args": ["src${/}app${/}index.js"]
}

For more on sharing configurations across your team, see how to share VS Code project settings with your team. To learn about task automation, read how to create and configure tasks.json.

Where variables work

Use variable substitution only where the configuration supports it:

  • launch.json: supported string keys and values defined by the selected debugger
  • tasks.json: only the command, args, and options fields
  • settings.json: selected settings, including certain terminal cwd, env, shell, and shellArgs values
  • .code-workspace files: the same supported configuration positions

Variables do not work in task labels or most settings. Check the current setting description or configuration IntelliSense when a value remains literal.

Rune AI

Rune AI

Key Insights

  • Use ${workspaceFolder} instead of machine-specific project paths.
  • Task substitution works only in command, args, and options.
  • Environment and configuration variables reuse existing values without hardcoding them.
  • Scope sibling roots with ${workspaceFolder:FolderName}.
  • Treat prompt and command variables as input that must be reviewed before execution.
RunePowered by Rune AI

Frequently Asked Questions

Why is my variable not being resolved in tasks.json?

Only command, args, and options support variable substitution in tasks. Labels and other fields do not. Input variables also cannot be nested inside other input variables.

What is the difference between ${workspaceRoot} and ${workspaceFolder}?

${workspaceRoot} is deprecated. Use ${workspaceFolder}, which supports the current multi-root workspace terminology.

Can I use variables in settings.json?

Only selected settings support predefined variables. Check the setting description in the current Settings editor instead of assuming substitution works in every string setting.

Conclusion

Use variables only in supported task, debug, and setting fields. Test the resolved value with a harmless echo task, keep secrets out of command arguments, and scope folder paths explicitly in multi-root workspaces.