Sublime Forum

How to bind multiple commands for a keybinding?

#1

In some situations I want to use a keybinding to run more than one command.
Just like to run a script, does sublime support this function?

1 Like

Unable to change build system in a keybinding
#2

It’s not possible by default. Temporary solution is to use this plugin: https://github.com/johnyluyte/sublime-text-multi-commads

E.g

  {
    "keys": [",", "f", "c"], "command": "run_multiple_commands",
    "context": [{"key": "setting.command_mode"}],
    "args": {
      "commands": [
        { "command": "fold" },
        { "command": "exit_visual_mode" }
      ]
    }
  },
1 Like

#3

This might also be something you can accomplish using Macros possibly.

1 Like

#4

Thank you Nilium, johnyluyte, and dvcrn for a fantastic simple plugin!

Thank you @oivva for pointing this out!

I am an experienced programmer, but I am new to sublime and did not have the time to learn the plugin system simply to execute a few window layout and group commands and bind them to a key. I tried doing this in a macro, but set_layout and friends are not supported in macros. I spent 2 hours trying to figure this out and failed to overcome it because it required far more investment in plugin magic than I have time for now.

Using multi-commands made this a trivial thing to do in my default keymap bindings file. Took 5 minutes!! :slight_smile:

1 Like

#6

its not working completely for me.
Just running the first two commands. not the bottom two.

[{“keys”: [“f1”], “command”: “run_multiple_commands”,
“args”: {“commands”: [
{ “command”: “select_all” },
{ “command”: “paste” },
{ “command”: “focus_group”, “args”: { “group”: 0 } },
{“command”: “build” }
]}}]

0 Likes

#7

The plugin linked above requires more configuration than you’re providing here in order to execute those commands. In particular, unless you include an extra context argument to specify what context the command should run in, it assumes it’s an text command.

None of the commands in your example have the context argument, so they are all assumed to be text commands, but the last two in fact are not, and hence they don’t run.

So one way to solve the issue would be to add modify the last two commands to include the appropriate context. For example:

{"command": "build", "context": "window" }

That said, both the Multicommand and Chain of Command packages are both able to do the same thing as this package, and they don’t require you to know or provide the context information.

Since that’s more of a technical thing and shouldn’t really be up to you to know, I’d recommend using one of them instead as an easier way to go. Multicommand is actually based on the code from the plugin above, so all you should need to do is change the command you’re running to multicommand instead of run_multiple_commands and your binding should work directly.

This video covers both packages with some examples if you’d like more information on either of them or how to use them:

1 Like