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?

1

3 Answers 3

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"
}
2
  • Is it possible to create a macro with arguments? I cannot find something like this in the macros extension documentation ...
    – bobeff
    Commented May 15, 2019 at 9:18
  • 2
    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.
    Commented Dec 21, 2019 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).

3
  • 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
    Commented Jul 14, 2020 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
    Commented Jul 14, 2020 at 18:18
  • Also wondering if there are any drawbacks to doing with the macros extension
    – katerlouis
    Commented Feb 11, 2023 at 20:31
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

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