How to bind one key to multiple commands in VSCode
Asked Answered
M

3

13

I'm trying to make the key Ctrl+UpArrow execute both commands cursorUp and scrollLineUp.

I was hoping that this would work, but it doesn't:

{
  "key": "ctrl+up",
   "command": ["cursorUp", "scrollLineUp"], // This doesn't work
   "when": "editorTextFocus"
}

How do I do that in VSCode?

Myrlemyrlene answered 8/3, 2018 at 15:41 Comment(1)
#75808871Appulse
F
13

This is currently not possible, but the corresponding feature request is tracked here. However you should take a look to the macros extension. It enables you to chain different commands to a single custom command. This custom command then can be bound to a hotkey. In your case you could add this to your settings.json:

"macros": {
    "myCustomCommand": [
        "cursorUp",
        "scrollLineUp"
    ]
}

And then add your custom hotkey to the keybindings.json:

{
  "key": "ctrl+up",
  "command": "macros.myCustomCommand"
}
Flabellate answered 8/3, 2018 at 17:36 Comment(2)
Is it possible to create a macro with arguments? I cannot find something like this in the macros extension documentation ...Surround
Passing Arguments to Commands Many commands accept arguments, like the "type" command which lets you insert text into the editor. For these cases use an object instead of a string when specifying the command to call in your settings.json: ``` "macros": { "addSemicolon": [ "cursorEnd", {"command": "type", "args": {"text": ";"}} ] } ```Deloresdeloria
P
7

There's a new way to achieve this without an extension:

  1. Run "Tasks: Open User Tasks" command to create or open a user level tasks file.

  2. Define commands as separate tasks, like so:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "ctrlUp1",
            "command": "${command:cursorUp}"
        },
        {
            "label": "ctrlUp2",
            "command": "${command:scrollLineUp}"
        },
        {
            "label": "ctrlUpAll",
            "dependsOrder": "sequence",
            "dependsOn": [
                "ctrlUp1",
                "ctrlUp2"
            ],
            "problemMatcher": []
        }
    ]
}
  1. In your keybindings.json:
{
    "key": "ctrl+up",
    "command": "workbench.action.tasks.runTask",
    "args": "ctrlUpAll",
    "when": "editorTextFocus"
}

("ctrlUpNNN" label format chosen for readability, task labels can be anything).

Penelope answered 26/4, 2020 at 12:34 Comment(3)
The configuration within keybindings.json semms to be wrong: You need to use "command": "workbench.action.tasks.runTask" and "args": "ctrlUpAll" AFAIK. Since this is also documented on the linked page, I will correct the answer.Glucosuria
Is this an improvement? 3 tasks or 1 macro. And if you had multiple of these x 3 every time... I'll use an extension.Appulse
Also wondering if there are any drawbacks to doing with the macros extensionEgoist
L
4

You can use the newly feature runCommands

e.g.

 {
    "key": "cmd+up",
    "command": "runCommands",
    "args": {
      "commands": ["cursorUp", "scrollLineUp"]
    },
    "when": "editorTextFocus"
  }

Reference: https://code.visualstudio.com/docs/getstarted/keybindings#_running-multiple-commands

Lemuelah answered 9/4, 2023 at 22:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.