VSCode environment variables besides ${workspaceRoot}

Be aware the ${workspaceRoot} variable has been deprecated in favor of the ${workspaceFolder} variable. It was deprecated (and no longer documented) in order to align better with Multi-root workspace support.

You can find the list at this link: https://code.visualstudio.com/docs/editor/variables-reference

For posterity reasons I’m going to list the variables (I’ve been trying to find them as well today), copying right from the link (and prettifying it), in case it ever changes again:

Visual Studio Code supports variable substitution in Debugging and Task configuration files. Variable substitution is supported inside strings in launch.json and tasks.json files using ${variableName} syntax.

Predefined variables

The following predefined variables are supported:

  • ${workspaceFolder} – the path of the folder opened in VS Code
  • ${workspaceFolderBasename} – the name of the folder opened in VS Code without any slashes (/)
  • ${file} – the current opened file
  • ${fileWorkspaceFolder} – the current opened file’s workspace folder
  • ${relativeFile} – the current opened file relative to workspaceFolder
  • ${relativeFileDirname} – the current opened file’s dirname relative to workspaceFolder
  • ${fileBasename} – the current opened file’s basename
  • ${fileBasenameNoExtension} – the current opened file’s basename with no file extension
  • ${fileDirname} – the current opened file’s dirname
  • ${fileExtname} – the current opened file’s extension
  • ${cwd} – the task runner’s current working directory on startup
  • ${lineNumber} – the current selected line number in the active file
  • ${selectedText} – the current 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

Note: The ${workspaceRoot} variable is deprecated in favor of the ${workspaceFolder} variable.

Environment variables

You can also reference environment variables through ${env:Name} syntax (for example, ${env:PATH})

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

Note: Be sure to match the environment variable name’s casing, for example ${env:Path} on Windows.

Settings and command variables

You can reference VS Code settings and commands using the following syntax:

  • ${config:Name} – example: ${config:editor.fontSize}
  • ${command:CommandID} – example: ${command:explorer.newFolder}

Variables scoped per workspace folder

By appending the root folder’s name to a variable (separated by a colon), it is possible to reach into sibling root folders of a workspace. Without the root folder name, the variable is scoped to the same folder where it is used.

For example, in a multi root workspace with folders Server and Client, a ${workspaceFolder:Client} refers to the path of the Client root.

Leave a Comment

tech