Skip to content

Macro like keybindings #871

Open
Open

Description

When creating keyboard shortcuts, it would be nice if you could pass an array of commands (to execute like a macro) that would run the commands in that order.

So take this for example:

{
    "key": "ctrl+s",
    "command": [
        "editor.action.format",
        "editor.action.trimTrailingWhitespace",
        "workbench.action.files.save"
    ],
    "when": "editorTextFocus"
}

It would format the file, remove trailing white space then save the file.

Activity

  1. added
    feature-request
    on Dec 1, 2015
  2. erichiller commented on Jul 8, 2016

    @erichiller

    Abolutely what I was thinking! - another example (mine)

    {
        "key": "winl+b",
        "command": [
            "workbench.action.tasks.terminate",
            "workbench.action.tasks.build"
        ]
    }
    
  3. geddski commented on Nov 15, 2016

    @geddski

    love this idea

  4. amkgo commented on Nov 15, 2016

    @amkgo

    Show any plain text in addition to commands will be great:

    For example, using ctrl+; to move to the end of line and add ; and then move to the next line.

    {
        "key": "ctrl+;",
        "commands": [
            "cursorEnd",
            ";",
            "editor.action.insertLineAfter"
        ],
        "when": "editorTextFocus"
    }
    
  5. fbnlsr commented on Feb 27, 2017

    @fbnlsr
    Contributor

    I'm surprised to see so few votes on this issue. I was looking for a way to bring up a terminal and hide the file explorer at the same time, since the integrated terminal doesn't occupy 100% width, and it seems vscode does'nt support multiple commands on a single keybindings yet. :(

    Also, #6617 for ref.

  6. faceyspacey commented on Mar 11, 2017

    @faceyspacey
  7. bstro commented on Mar 15, 2017

    @bstro
  8. liuhaopen commented on Mar 17, 2017

    @liuhaopen
  9. ThisIsManta commented on Mar 18, 2017

    @ThisIsManta

    Hi guys, I've just found out the extension macros which is exactly I was looking for. Hope this helps you overcome this limitation.

  10. dvf commented on May 10, 2017

    @dvf

    You probably want to allow arguments, for example when using the type command:

    {
        "key": "ctrl+;",
        "commands": [
            {
                 "command": "cursorEnd"
            },
            {
                 "command": "type",
                 "args": { "text": ";" }
            }
        ]
        "when": "editorTextFocus"
    }
    
  11. 51 remaining items

  12. vec715 commented on Mar 7, 2023

    @vec715
  13. a-pav commented on Mar 8, 2023

    @a-pav

    I think that @ liangjiancang's suggestion above about the ability to define "key" as an array is also valid. It can save us from copy-pasting for just a minor change.

  14. ulugbekna commented on Apr 3, 2023

    @ulugbekna
    Contributor

    Hello everyone! 👋

    VS Code team engineer here 🙂

    Just wanted to let you know that we added a built-in command runCommands that can run multiple commands sequentially. This is not first-class support for macros as described in the feature request (this feature request remains open), but this should be quite a handy command, especially given it supports auto-completion/linting for command name and command argument names.

    We encourage people to try it out and report issues/suggest improvements in new issues (there is now a GitHub issue label runCommands)

    You can read more about the command here: https://code.visualstudio.com/docs/getstarted/keybindings#_running-multiple-commands

    Make sure you're on latest VS Code version - 1.77.

  15. EugeneDae commented on Apr 3, 2023

    @EugeneDae

    Ulugbek Abdullaev (@ulugbekna) That's a great news, thanks so much!

  16. sandeepkambham08 commented on Apr 4, 2023

    @sandeepkambham08

    Tested and it works 😉
    Thank you 👍

  17. jhuitema commented on Apr 18, 2023

    @jhuitema

    Thanks, this works great!

    Would it be possible to add an argument to the runCommands action to optionally compress the commands into a single Undo action? If that is a big ask I can create a new issue for it.

    For example, I am using a "Join Lines without inserting whitespace" key binding like so:

        {
            "key": "ctrl+shift+j",
            "command": "runCommands",
            "args": {
                "commands": [
                    "editor.action.joinLines",
                    "deleteRight"
                ]
            },
            "when": "textInputFocus"
        }

    This works great but to undo it you need to Undo twice. Being able to only Undo once would be ideal.

  18. nikivdev commented on Nov 11, 2023

    @nikivdev

    Hello everyone! 👋

    VS Code team engineer here 🙂

    Just wanted to let you know that we added a built-in command runCommands that can run multiple commands sequentially. This is not first-class support for macros as described in the feature request (this feature request remains open), but this should be quite a handy command, especially given it supports auto-completion/linting for command name and command argument names.

    We encourage people to try it out and report issues/suggest improvements in new issues (there is now a GitHub issue label runCommands)

    You can read more about the command here: https://code.visualstudio.com/docs/getstarted/keybindings#_running-multiple-commands

    Make sure you're on latest VS Code version - 1.77.

    I just tried it with this:

      {
        "key": "cmd+shift+;",
        "command": "runCommands",
        "args": {
          "commands": [
            "breadcrumbs.focus",
            "cursorLeft"
          ]
        }
      },
    

    And it failed. It did not do arrow left after breadcrumbs were focused.

  19. a-pav commented on Nov 15, 2023

    @a-pav
    "commands": [
            "breadcrumbs.focus",
            "cursorLeft"
          ]
    

    Instead of cursorLeft, you can use breadcrumbs.focusPrevious.

  20. ATXadam commented on Feb 17, 2024

    @ATXadam

    Having an issue with workbench.action.open*Settings* commands in runCommands. While it does work, it does throw an error, and prevents further execution of commands from running.

    What I'm trying to accomplish is opening the settings.json files on keybind. If I run one of these commands as a root action for the keybind, it does work without issue.

    Example Keybind:

    [
        {
            "key": "ctrl+shift+/",
            "command": "runCommands",
            "args": {
                "commands": [
                    "workbench.action.openApplicationSettingsJson",
                    "workbench.action.openSettingsJson"
                ]
            }
        }
    ]

    When executing the keybind, I get a VS Code alert:

    Converting circular structure to JSON
        --> starting at object with constructor 'Y'
        |     property 'C' -> object with constructor 'Array'
        |     index 0 -> object with constructor 's'
        |     ...
        |     index 1 -> object with constructor 'a'
        --- property 'j' closes the circle
        */
    

    As well as the following Output -> Window log:

    2024-02-17 08:26:04.812 [error] Method not found: toJSON: CodeExpectedError: Method not found: toJSON
        at Object.call (C:\Program Files\Microsoft VS Code\resources\app\out\vs\code\electron-main\main.js:42:5090)
        at C.s (C:\Program Files\Microsoft VS Code\resources\app\out\vs\code\electron-main\main.js:40:5128)
        at C.q (C:\Program Files\Microsoft VS Code\resources\app\out\vs\code\electron-main\main.js:40:4644)
        at $.value (C:\Program Files\Microsoft VS Code\resources\app\out\vs\code\electron-main\main.js:40:4051)
        at h.y (C:\Program Files\Microsoft VS Code\resources\app\out\vs\code\electron-main\main.js:35:1902)
        at h.z (C:\Program Files\Microsoft VS Code\resources\app\out\vs\code\electron-main\main.js:35:1972)
        at h.fire (C:\Program Files\Microsoft VS Code\resources\app\out\vs\code\electron-main\main.js:35:2188)
        at $.value (C:\Program Files\Microsoft VS Code\resources\app\out\vs\code\electron-main\main.js:33:28530)
        at h.y (C:\Program Files\Microsoft VS Code\resources\app\out\vs\code\electron-main\main.js:35:1902)
        at h.fire (C:\Program Files\Microsoft VS Code\resources\app\out\vs\code\electron-main\main.js:35:2119)
        at $.value (C:\Program Files\Microsoft VS Code\resources\app\out\vs\code\electron-main\main.js:33:28733)
        at h.y (C:\Program Files\Microsoft VS Code\resources\app\out\vs\code\electron-main\main.js:35:1902)
        at h.fire (C:\Program Files\Microsoft VS Code\resources\app\out\vs\code\electron-main\main.js:35:2119)
        at ge (C:\Program Files\Microsoft VS Code\resources\app\out\vs\code\electron-main\main.js:33:31158)
        at IpcMainImpl.l (C:\Program Files\Microsoft VS Code\resources\app\out\vs\code\electron-main\main.js:42:18908)
        at IpcMainImpl.emit (node:events:514:28)
        at WebContents.<anonymous> (node:electron/js2c/browser_init:2:79287)
        at WebContents.emit (node:events:514:28)
    

    With VS Code Web, I do get a different alert: cyclic object value

    With the following log in console:

    TypeError: cyclic object value
        run commands.contribution.ts:106
        handler actions.ts:592
        handler commands.ts:98
        invokeFunction instantiationService.ts:68
        n commandService.ts:95
        executeCommand commandService.ts:60
        M abstractKeybindingService.ts:372
        J abstractKeybindingService.ts:225
        W keybindingService.ts:281
        d dom.ts:136
        a dom.ts:157
        W keybindingService.ts:273
        x keybindingService.ts:244
        ie event.ts:621
        x keybindingService.ts:244
        j instantiationService.ts:119
        t instantiationService.ts:245
        s instantiationService.ts:234
        r instantiationService.ts:223
        q instantiationService.ts:163
        m instantiationService.ts:147
        get instantiationService.ts:61
        ob layout.ts:306
        startup workbench.ts:155
        invokeFunction instantiationService.ts:68
        startup workbench.ts:146
        open web.main.ts:131
        m web.factory.ts:62
        main (index):9
        i (index):9
        i (index):9
        97 (index):9
        a (index):9
        <anonymous> (index):9
        <anonymous> (index):9
    notificationsAlerts.ts:40:13
    

    This is without any extensions running (fresh profile). Produced identical results with no extensions and a default profile:

    • Windows_NT x64 10.0.22631
      • Installed App 1.86.2 (system setup) (Commit: 903b1e9)
      • Portable App 1.86.2 (Commit: 903b1e9)
    • VS Code Web: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0
      • Version: 1.86.2 (Commit: 903b1e9)
      • Version: 1.87.0-insider (Commit: 00124e9)

    Also, it would be great (and enough to get me by) if there was a way to execute commands and continue regardless of failure? In my example, I may not have a workspace open, so if I opened Application -> Workspace -> User, and no workspace, user would also not open.

  21. vanowm commented on May 2, 2024

    @vanowm

    Just wanted to let you know that we added a built-in command runCommands that can run multiple commands sequentially. This is not first-class support for macros as described in the feature request (this feature request remains open), but this should be quite a handy command, especially given it supports auto-completion/linting for command name and command argument names.

    We encourage people to try it out and report issues/suggest improvements in new issues (there is now a GitHub issue label runCommands)

    I'm confused, was it easier to implement completely new command, rather than check for type of value in existing command, and if it's a string execute it, if it's an array execute each item in the array?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

feature-requestkeybindings

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions