Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode environment variables besides ${workspaceRoot}

Tags:

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}.

like image 300
ZEE Avatar asked Aug 01 '16 16:08

ZEE


People also ask

How do you change Environment Variables in VS Code?

If you open the . vscode/launch. json file in your workspace or select Debug > Open Configurations then you should see a set of launch configurations for debugging your code. You can then add to it an env property with a dictionary of string:string.

What is ${ workspaceFolder in VS Code?

${workspaceFolder} - the path of the folder opened in VS Code.

What is ${ fileDirname?

Using VS Code variables ${workspaceFolder} refers to the root of the workspace opened in VS Code. ${file} refers to the currently opened file. ${fileDirname} refers to the directory containing the currently opened file.

How do I create an .ENV File in Visual Studio Code?

Once you have opened the folder, click on the Explorer icon on the top left corner of the VSCode (or press Ctrl+Shift+E) to open the explorer panel. In the explorer panel, click on the New File button as shown in the following screenshot: Then simply type in the new file name . env ...


2 Answers

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.

like image 57
Luminous Avatar answered Sep 23 '22 05:09

Luminous


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}.

like image 20
seairth Avatar answered Sep 20 '22 05:09

seairth