Visual Studio Code keybindings - Running two or more commands with one shortcut
Asked Answered
T

3

80

I have the following keybinding in VS Code which toggles the position of the cursor between the active document and built-in terminal:

  // Toggle between terminal and editor focus
{
    "key": "oem_8",
    "command": "workbench.action.terminal.focus"
},
{
    "key": "oem_8",
    "command": "workbench.action.focusActiveEditorGroup",
    "when": "terminalFocus"
}

Before i click the shortcut key to move the cursor to the terminal, i first have to save the active file.

I would therefore like to run the file saving command, which after searching on google i believe is workbench.action.files.save

How would i do this? I have tried adding the above code snippet at the end of the "command" line but it has not worked.

Terzetto answered 2/4, 2018 at 12:36 Comment(1)
Possible duplicate of Multiple actions on one keyboard shortcut in vscodeEthical
M
119

Update: released for vscode v1.77, more at run multiple commands like a macro.

You are able to do this:

{
  "command": "runCommands",
  "key": "alt+r", // whatever keybinding
  "args": {
    "commands": [
      // commands to run in sequence
      "workbench.action.files.save",
      "workbench.action.terminal.focus"
    ]
  }
}

The command runCommands is built-in, so no extension is necessary for your use case. But see the link above, some use cases might require a macro extension.


Previous answer:

You would need a macro extension to run multiple commands from one keybinding.

I now use multi-command and there are other macro extensions now.

You can use this keybinding (in your keybindings.json) with the multi-command extension - no need for anything in settings.json:

{
  "key": "oem_8", // or whatever keybinding you wish
  "command": "extension.multiCommand.execute",
  "args": {
    "sequence": [
      "workbench.action.files.save",
      "workbench.action.terminal.focus"
    ]
  },
  "when": "editorTextFocus" // if you want this, you probably do
}

If you have more complicated macros you can still build them in your settings.json if you wish.

Mop answered 2/4, 2018 at 13:49 Comment(10)
I found out that the "macros" object didn't work in settings.json but when I changed the key to "macros.list" it workedRellia
can you show a complex macro in setting.jsonConvenience
It worth mentioning that it is possible to use sequence of commands, each with its own arguments. Example in documentation: github.com/ryuta46/…Alvarez
command 'extension.multiCommand.execute' not foundSwabber
There's a little page with an arrow wrapping around it at the top right of the GUI to click to open the JSON file in case anyone is wondering how to access the JSON file.Hearken
What would be oem_8 key? Where to find the key string representation map? ThxWhin
Is it possible to add different "when" clauses for each command in the sequence? Like doing a switch case block?Cubiform
@SamwiseGanges No I don't think you can specify different when clauses - I see nothing about that in the documentation. But you might be able to overload the same keybinding in two different keybindings to achieve what you want.Mop
for the VS Code 1.77 method, how can one pass arguments to the commands in the list?Autoicous
@user See the link I mentioned: #75808871 There are a couple of examples of commands with args there.Mop
A
18

There is a way to run a sequence of commands without any extensions. It is by using Tasks. I like this method, because it allows to define a command, that is in workspace scope, not in global scope. The idea was taken from this blog post. Example of tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "cmd-1",
            "command": "${command:workbench.action.files.save}"
        },
        {
            "label": "cmd-2",
            "command": "${command:workbench.action.terminal.focus}"
        },
        {
            "label": "cmd-All",
            "dependsOrder": "sequence",
            "dependsOn": [
                "cmd-1",
                "cmd-2"
            ],
        }
    ]
}

Then in keybindings.json you just bind your hotkey to the task:

{
    "key": "oem_8",
    "command": "workbench.action.tasks.runTask",
    "args": "cmd-All"
}

Note, that when defining a task, you can pass arguments to the command with the help of input variables.

Alvarez answered 11/5, 2022 at 13:31 Comment(2)
What would be oem_8 key? Where to find the key string representation map? ThxWhin
Not sure where you can find the list. Try to set some shortcut graphically, then switch to json view and see how that shortcut is represented.Alvarez
E
5

Another extension to run multiple commands: Commands

{
    "key": "oem_8",
    "command": "commands.run",
    "args": [
        "workbench.action.files.save",
        "workbench.action.terminal.focus"
    ],
    "when": "editorTextFocus"
}

I made this extension. It's great.

Ethical answered 2/7, 2021 at 18:13 Comment(2)
The extension is great but you should add to documentation that you can put commands directly into keybindings.json. I was trying to get it to work by putting it into settings.json as is in all the documentation examples.Accelerometer
There's a Miscellaneous link in README.md of the extension.Ethical

© 2022 - 2024 — McMap. All rights reserved.