Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Yes, it is possible to perform variable expansion in keybindings.json and tasks.json/inputs when using VSCode.

In keybindings.json, you can use the ${var} syntax to reference any variable defined in the workspace or the user settings. For example, you could define a variable myFolderPath in the user settings, and reference it in a keybinding like this:

{
    "key": "ctrl+alt+o",
    "command": "workbench.action.openFolder",
    "args": {"fileUri": "${myFolderPath}"}
}

In tasks.json and inputs, you can use the ${input:...} syntax to prompt the user for input values, and the ${...} syntax to reference other variables or environment variables. For example, you could prompt the user for an input value and use it in a task like this:

{
    "label": "Copy file",
    "type": "shell",
    "command": "cp",
    "args": [
        "${fileToCopy}",
        "${destinationFolder}"
    ],
    "inputs": [
        {
            "id": "fileToCopy",
            "type": "promptString",
            "description": "Enter the path of the file to copy"
        },
        {
            "id": "destinationFolder",
            "type": "promptString",
            "description": "Enter the path of the destination folder"
        }
    ]
}

Note that variable expansion is not supported in all areas of VSCode, so be sure to check the documentation for the specific feature you are using to see if variable expansion is supported.