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?

HaaLeo
  • 10,065
  • 3
  • 44
  • 55
Ameen
  • 1,747
  • 3
  • 16
  • 31
  • https://stackoverflow.com/questions/75808371/how-can-i-run-multiple-commands-with-a-single-vs-code-keybinding-without-an-exte/75808372#75808372 – Mark Apr 10 '23 at 00:28

3 Answers3

12

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"
}
HaaLeo
  • 10,065
  • 3
  • 44
  • 55
  • Is it possible to create a macro with arguments? I cannot find something like this in the macros extension documentation ... – bobeff May 15 '19 at 09:18
  • 1
    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": ";"}} ] } ``` – Thai D. V. Dec 21 '19 at 16:24
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).

doak
  • 809
  • 9
  • 24
Dae
  • 2,345
  • 2
  • 22
  • 34
  • 1
    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. – doak Jul 14 '20 at 17:13
  • 1
    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. – Mark Jul 14 '20 at 18:18
  • Also wondering if there are any drawbacks to doing with the macros extension – katerlouis Feb 11 '23 at 20:31
2

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

KhaledMohamedP
  • 5,000
  • 3
  • 28
  • 26