Ask Your Question
4

Is it possible to perform variable expansion in keybindings.json or in tasks.json/inputs when using vscode?

asked 2022-12-05 11:00:00 +0000

lalupa gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-07-30 22:00:00 +0000

nofretete gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-12-05 11:00:00 +0000

Seen: 7 times

Last updated: Jul 30 '21