T. Lau
T. Lau

Reputation: 39

VisualStudio Code Tasks and Variables

Visual studio 2010 had an option when creating external tools to define an initial directory like $(ItemDir) which would be based on the file currently in focus. I am trying to do something similar in visual studio code but I cannot find an equivalent environment variable to do the same thing. Any suggestions would be much appreciated.

Upvotes: 1

Views: 1655

Answers (1)

Mark
Mark

Reputation: 182076

Just to formalize the info into an answer:

Here is a list of predefined variables that can be used in tasks: Variables reference

and here is more info on using variables in tasks : Variable substitution

And this is really interesting from the last link above:

Similarly, you can reference your project's configuration settings by prefixing the name with ${config:.

For example, ${config:python.pythonPath} returns the Python extension setting pythonPath.

Below is an example of a custom task configuration which executes autopep8 on the current file using your project's selected Python executable:

{
        "label": "autopep8 current file",
        "type": "process",
        "command": "${config:python.pythonPath}",
        "args": [
            "-m",
            "autopep8",
            "-i",
            "${file}"
        ]
    }

From the first link we see that you can even incorporate commands (like those used in keybindings and the command palette) into your tasks:

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}

Finally, related but because some people might end up here searching for these, there are variables that can be used in snippets (but not tasks). See Snippet Variables

Upvotes: 2

Related Questions