28

In vscode, I have a launch.json with more than one configuration.

Is there a way to assign a keyboard shortcut to a specific configuration?, I've not been able to find any information, thanks!

tru7
  • 6,348
  • 5
  • 35
  • 59

2 Answers2

31

Almost, but not quite.

To my knowledge there is no vs-code command to start debugging a given launch configuration.

What you can do instead is point a keyboard shortcut at the workbench.action.debug.selectandstart command which will pop-up a dialogue for you to select the configuration and hit enter. In practice this can be done very quickly as you only need to start typing the first letter or two of your launch config or use the arrows

screengrab

To enable this hit Ctrl+kCtrl+s (at least this is the default shortcut on windows, you can always search for 'keybindings' in the command palette if this doesn't work for you).

Search for the workbench.action.debug.selectandstart command then right-click or click the edit icon to change the keybinding:

screengrab2

Stewart_R
  • 13,764
  • 11
  • 60
  • 106
13

Update: I just created an extension Launch Configs which allows you to set up a keybinding for any launch configuration in your launch.json. Multiple keybindings for different launch configs if you wish. Example setting (in your settings.json):

 "launches": {

  "RunNodeCurrentFile": "Launch File",
  "RunCompound1": "Launch file and start chrome"
},

So with the extension you can set the value to the name of your desired launch.json configuration to run/debug. And then keybindings to trigger those configurations (in your keybindings.json).

{
  "key": "alt+f",
  "command": "launches.RunNodeCurrentFile"
},
{
  "key": "alt+g",
  "command": "launches.RunCompound1"
}

See my answer at https://github.com/microsoft/vscode/issues/97921

This keybinding works:

{
  "key": "alt+j",                            // whatever keybinding you like
  "command": "debug.startFromConfig",
  "args": {
      "type": "node",
      "request": "launch",
      "name": "First Debugger",
      "program": "${workspaceFolder}/test.js",
      "console": "integratedTerminal",
      //"preLaunchTask": "echo Builtin Variables"  // can't get it to find a pre-launch task
  }
}

but unfortunately not by simply referring to an existing config in your launch.json - by name for example. The referred config - here First Debugger can, but doesn't have to be in your launch.json. In any case all the args must appear in your keybinding.

Mark
  • 143,421
  • 24
  • 428
  • 436
  • Extension is not working when configs are created globally within the settings.json like "launch": {} – MohitGhodasara Jan 04 '21 at 05:26
  • Please file an issue on the github page with the code you tried. Thank you. And it is `"launches"` not `"launch"`. – Mark Jan 04 '21 at 05:38
  • I am talking about the "launch" key that is used to add a global config. after that, you don't need to add launch.json to your project – MohitGhodasara Jan 04 '21 at 06:21
  • @Mark Thank you for the extension. I got it to work with two different configurations in launch.json, but both times it starts to debug. How can I instead *run* without debugging with the configuration? – azureai Dec 02 '21 at 21:22
  • @azureai It sounds like you would like to add the option to run this command: `workbench.action.debug.run` instead of the `start/stop/restart debugging` options? – Mark Dec 02 '21 at 22:19
  • @Mark Sorry, I don't know what you mean. I just want to run the Python file instead of debugging it. Debugging is slow and stops at breakpoints, running is fast. I have added "launches": {"RunExperiment": "Run my_file.py"} to my settings.json, and "Run my_file.py" is also the name of my configuration in launch.json. I also added the corresponding hotkey. Now i click the hotkey and VSCode starts debugging, halting at breakpoints. What do I need to change to run instead of debug? I'm new to VSCode, it's easier in PyCharm. – azureai Dec 10 '21 at 23:04
  • @azureai How would you normally Run without debugging? What command or key combo would you trigger? – Mark Dec 10 '21 at 23:18
  • Thansks. You may add a tip that `"launches"` should be inserted into `settings.json`, but not `.vscode/launch.json` - which took me several minutes to find why it didn't work. – Dahan Gong Apr 22 '22 at 08:03
  • @DahanGong Thanks for the tip, I added a couple of pointers to the answer about `settings.json` and `keybindings.json`. – Mark Apr 22 '22 at 17:55