26

I know there is ${workspaceRoot} environment variable available. What other environment variables are there to use?

One that would be of particular interest would be the filename without the ${workspaceRoot} part with all \ chars replaced with / so we can use this as a url builder. Then you could use a URL like "http://localhost:9876/${relativeFile}".

It would be really helpful if there is something like a ${relativeFile} and a ${relativeFolder}.

2
  • 3
    You can find full list of currently defined variables here: Variables Reference
    – Romko
    Jan 24, 2018 at 13:46
  • 2
    for posterity, it appears that ${workspaceRoot} has now been changed to ${workspaceFolder} as of VSCode v.1.19
    – Gifford N.
    Jan 29, 2018 at 21:11

2 Answers 2

41

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.

8
  • 1
    This info is already collected here: stackoverflow.com/questions/49304183/visualstudio-code-tasks/…
    – Mark
    May 7, 2018 at 14:13
  • 4
    @Mark Using only links as an answer is poor quality (like in the 2 other answers). Actually displaying the information gives people the ability to come to one place to find what they are looking for without being redirected to some other site. Not saying your answer is bad, it actually does what I just described as a good answer.
    – Luminous
    May 7, 2018 at 15:36
  • 1
    This particular info changes almost monthly, so that is a concern. I would never stop at an answer posted here to this sort of open-ended question - what variables are there besides the one mentioned - without going to a link.
    – Mark
    May 7, 2018 at 16:12
  • ${activeEditorLanguage} is missing from the list.
    – mbomb007
    Apr 22, 2020 at 21:17
  • 1
    Where can I output the env vs code variables, it doe not work in terminal or console output?
    – Timo
    May 21, 2021 at 16:04
6

You can find a list of available substitution variables here:

https://code.visualstudio.com/docs/editor/tasks#_variable-substitution

Edit: The full list can actually be found in the systemVariables.ts source file. The base class defines a resolve() method that uses a regular expression to replace matches with string property values with the same name. Notice that SystemVariables also includes all process.env values, where the pattern is ${env.KEY}.

4
  • Thanks... but that variables are for 'Tasks' not for 'Builds'.... I've analyzed the code in github and I only found one more var besides the one i mentioned in the question...
    – ZEE
    Aug 1, 2016 at 23:36
  • 1
    @ZEE I updated the post to give a bit more detail. Since I don't know where you were looking in the code, I don't know whether that code is making use of the SystemVariables class or not.
    – seairth
    Aug 2, 2016 at 2:54
  • 1
    Github link is broken, variable substitution page does not define workspaceRoot
    – Dagrooms
    Dec 8, 2017 at 20:45
  • They seem to be here now github.com/microsoft/vscode/blob/…
    – E. Sundin
    Aug 20, 2019 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.