Jonathan Corwin
Jonathan Corwin

Reputation: 1040

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.

Upvotes: 67

Views: 39380

Answers (4)

Frank N
Frank N

Reputation: 10396

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

Upvotes: 25

Dirk Bäumer
Dirk Bäumer

Reputation: 1191

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 shortcuts to tasks.

Upvotes: 15

tdhsmith
tdhsmith

Reputation: 841

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

Upvotes: 68

user1132959
user1132959

Reputation: 988

I just submitted a PR for this: https://github.com/Microsoft/vscode/pull/10676

So once it's merged you will have the ability to assign any task to a keyboard shortcut.

Upvotes: 8

Related Questions