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.

1

3 Answers 3

118

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.

10
  • 1
    I found out that the "macros" object didn't work in settings.json but when I changed the key to "macros.list" it worked
    – user14029620
    Commented Jan 31, 2021 at 21:40
  • 1
    can you show a complex macro in setting.json
    – Esqarrouth
    Commented Mar 20, 2021 at 17:37
  • 4
    It worth mentioning that it is possible to use sequence of commands, each with its own arguments. Example in documentation: github.com/ryuta46/…
    – Ashark
    Commented May 6, 2022 at 10:06
  • 1
    @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.
    – Mark
    Commented Feb 19, 2023 at 22:28
  • 1
    @user See the link I mentioned: stackoverflow.com/questions/75808371/… There are a couple of examples of commands with args there.
    – Mark
    Commented Apr 6, 2023 at 22:21
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.

2
  • What would be oem_8 key? Where to find the key string representation map? Thx
    – artu-hnrq
    Commented Dec 10, 2022 at 6:18
  • 1
    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.
    – Ashark
    Commented Dec 11, 2022 at 19:34
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.

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. Commented Oct 24, 2021 at 2:39
  • There's a Miscellaneous link in README.md of the extension.
    – Alex
    Commented Oct 25, 2021 at 10:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.