Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Macro like keybindings #871

Open
TheColorRed opened this issue Dec 1, 2015 · 68 comments
Open

Macro like keybindings #871

TheColorRed opened this issue Dec 1, 2015 · 68 comments
Assignees
Labels
feature-request Request for new features or functionality keybindings VS Code keybinding issues
Milestone

Comments

@TheColorRed
Copy link

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.

@alexdima alexdima added the feature-request Request for new features or functionality label Dec 1, 2015
@egamma egamma modified the milestone: Backlog Dec 10, 2015
@alexdima alexdima added the keybindings VS Code keybinding issues label Mar 17, 2016
@erichiller
Copy link

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

{
    "key": "winl+b",
    "command": [
        "workbench.action.tasks.terminate",
        "workbench.action.tasks.build"
    ]
}

@alexdima alexdima removed their assignment Aug 30, 2016
@geddski
Copy link

geddski commented Nov 15, 2016

love this idea

@amkgo
Copy link

amkgo commented Nov 15, 2016

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"
}

@fbnlsr
Copy link
Contributor

fbnlsr commented Feb 27, 2017

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.

@faceyspacey

This comment was marked as duplicate.

2 similar comments
@bstro

This comment was marked as duplicate.

@liuhaopen

This comment was marked as duplicate.

@ThisIsManta
Copy link

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

@dvf
Copy link

dvf commented May 10, 2017

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

{
    "key": "ctrl+;",
    "commands": [
        {
             "command": "cursorEnd"
        },
        {
             "command": "type",
             "args": { "text": ";" }
        }
    ]
    "when": "editorTextFocus"
}

@drKnoxy
Copy link

drKnoxy commented Nov 13, 2017

If anyone on the core team would flag this with "help wanted" or "good first issue" i'd love to help out !

@weeksdev
Copy link

Ok so this isn't really answer to issue in vscode, but one work around i found was to use autohotkey.

@fbnlsr
Copy link
Contributor

fbnlsr commented Nov 23, 2017

FYI, I know this is slightly off-topic but for anyone using their machine to code and as a gaming platform: autohotkey can trigger video games anti-cheat programs. If you don't want to get kicked/banned during your next session of battlefield, be sure to have autohotkey turned off before playing. :)

@isaiahtaylor

This comment was marked as duplicate.

@jsardev

This comment was marked as duplicate.

@ItsCubeTime
Copy link

Bind one key to multiple commands via user level tasks, no extensions required. Not a perfect way, but it works.

Theres no way of running commands like: editor.action.insertSnippet
in tasks right?

@usernamehw
Copy link
Contributor

usernamehw commented Jun 19, 2021

Created another extension that can run multiple commands:

https://marketplace.visualstudio.com/items?itemName=usernamehw.commands

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

@ItsCubeTime
Copy link

Created another extension that can run multiple commands:

https://marketplace.visualstudio.com/items?itemName=usernamehw.commands

Looks great 😀

@bersbersbers
Copy link

@usernamehw
Copy link
Contributor

Yes, but like, better with more features.

@vec715

This comment was marked as duplicate.

@a-pav
Copy link

a-pav commented Mar 8, 2023

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.

@ulugbekna
Copy link
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.

@EugeneDae
Copy link

@ulugbekna That's a great news, thanks so much!

@sandeepkambham08
Copy link

sandeepkambham08 commented Apr 4, 2023

Tested and it works 😉
Thank you 👍

@jhuitema
Copy link

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.

@nikitavoloboev
Copy link

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.

@a-pav
Copy link

a-pav commented Nov 15, 2023

"commands": [
        "breadcrumbs.focus",
        "cursorLeft"
      ]

Instead of cursorLeft, you can use breadcrumbs.focusPrevious.

@ulugbekna ulugbekna added this to the Backlog milestone Dec 6, 2023
@ATXadam
Copy link

ATXadam commented Feb 17, 2024

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.

@vanowm
Copy link

vanowm commented May 2, 2024

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
Labels
feature-request Request for new features or functionality keybindings VS Code keybinding issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.