17

Currently, I have a Build Task set up in Visual Studio Code (not Visual Studio). When I press Ctrl+Shift+B, I get a list of my build tasks, I then have to select my task and then it will compile and run my program.

Is there an easier way to do this, so instead of Ctrl+Shift+B -> Enter, I can just press one button and have a preset Build Task run? Either a keyboard button or a GUI button will work great.

Gama11
  • 31,714
  • 9
  • 78
  • 100
Aaron Franke
  • 3,268
  • 4
  • 31
  • 51

2 Answers2

25

Mark the task as your default build task via Terminal -> Configure Default Build Task... This simply adds the following to the task in tasks.json:

"group": {
    "kind": "build",
    "isDefault": true
}

After that, Ctrl+Shift+B will run the task directly.

Additionally, you could also have a default test task with "kind": "test". That task can be directly run with the Tasks: Run Test Task command (no shortcut assigned by default).

And finally, if having two shortcuts is still not enough (or you don't want to modify tasks.json), you can set up keybindings to run tasks directly by their name:

{
    "key": "ctrl+b",
    "command": "workbench.action.tasks.runTask",
    "args": "run"
}

Replacing run with the label of your Build Task.

To open keybindings.json press Ctrl+K Ctrl+S or click File -> Preferences -> Keyboard Shortcuts. You may need to add [] if the file was previously empty.

Gama11
  • 31,714
  • 9
  • 78
  • 100
  • First of all thank you, I'm now down to 3 button presses instead of 4 and I can do it with one hand. Where is `keybindings.json`? I tried adding it to my project's `.vscode` folder where the `tasks.json` file is, but it doesn't seem to do anything. – Aaron Franke Jan 11 '19 at 11:01
  • Ok I figured it out. Press Ctrl+K and Ctrl+S to open it, then add this, and the part you need to change is the "args" section. – Aaron Franke Jan 11 '19 at 11:05
  • 1
    @AaronFranke Are you able to set keybindings within your workspace in the `.vscode` folder? – Jean-Francois T. Apr 29 '20 at 16:43
  • 1
    Follow up question: Once you have created a default build task, how do you trigger *non*-default build tasks? – Lucretiel Aug 16 '20 at 17:39
1

You can add this code in keybindings.json located at C:\Users\%User%\AppData\Roaming\Code\User\:

[
   {
      "key": "ctrl+shift+r",
      "command": "workbench.action.tasks.runTask",
      "args": "run"
   },
   // [...]
]

Source: https://lronaldo.github.io/cpctelera/files/buildsys/vscode_integration-txt.html

uri
  • 31
  • 1