Is it possible to assign different shortcuts to different tasks in VS Code?

VS Code allows multiple tasks to be defined in the ["tasks"] array in tasks.json and the one with the property isBuildCommand: true gets given the keyboard shortcut Ctrl+Shift+B by default.

I would like to assign different keyboard shortcuts to each of the tasks I've created. Is this possible, and if so how?

All I've found so far is the ability to assign a shortcut to the command workbench.action.tasks.runTask which will popup a menu of all the tasks in alphabetical order that I can up/down arrow through. I would like to configure Code to run each task directly with one key combination.


Solution 1:

As of VS Code 1.10, you can use the workbench.action.tasks.runTask command in your keybindings, and pass in the task's name as your argument.

The VS Code task documentation gives this example:

{
    "key": "ctrl+h",
    "command": "workbench.action.tasks.runTask",
    "args": "build"
}

Solution 2:

As of VSCode 1.19 (Feb 2018):

In /myproject/.vscode/tasks.json you need to add a label (formerly, now deprecated: taskName) to your npm task.

(I named my label the same as the package.json script I intend to run. But that's just personal style, not technical need):

    {
        "label": "ui",
        "type": "npm",
        "script": "ui"
    }

Then you reference that task by its label in your user keybindings /home/johndoe/.config/Code/User/keybindings.json:

{"key": "ctrl+r",
    "command": "workbench.action.tasks.runTask",
    "args": "ui"
},

In case you wondered: No, there are no project-level keybinding in vscode. Reasons here

Solution 3:

Yes there is one other property that can be used to bind a shortcut. Its name is isTestCommand. If set to true it bind Ctrl+Shift+T to the task. We do have an internal work item to allow to bind arbitrary short cuts to tasks.