Sublime Forum

Multiple Key Bindings to One Key /w Dropdown?

#1

Is there anyway to bind two commands to one shortcut and have both shortcuts appear as a dropdown to select which command you want to run?

I find myself spending a lot of time setting up shortcuts yet forgetting the key combos so I rather start utilizing a specific set of shortcuts which give me the option to select which command to run… any ideas?

0 Likes

#2

Kinda. You have two options really.

  1. Command Palette. If you can’t remember a specific shortcut, most commands are available from the command palette, which is essentially just a dropdown of commands. If a specific command isn’t in the palette, you can add them with a sublime-commands file.

  2. Key Sequences. You can assign multiple commands to the same initial key in a sequence. For example, you could have a key sequence set to “ctrl+b”,“ctrl+m” and then another key sequence set to “ctrl+b”,“ctrl+n.” Keysequencing allows for using multiple, easy to remember shortcuts instead of longer, more complex shortcuts. Something interesting, though, is key sequences don’t require modifiers. So feel free to set the following to a sequence: “ctrl+u”,f.

P.S. the keybindings I gave you are not valid because they already of a non-sequenced keybinding attached.

0 Likes

#3

You can also do something like that:

class YourCommand(sublime_plugin.WindowCommand):
  def run(self):
    self.menu = "1. Checkout", "2. New branch", "3. Rename branch", "4. Delete branch" ...]
    self.window.show_quick_panel(menu, self.command_selected)

   def command_selected(self, selected_index):
     ...

Then you bind a hotkey to your_command et voila. I prefixed the commands with numbers, because this works well with fuzzy matching implemented in Sublime’s quick panels. So, to checkout a branch, I just press my shortcut, then 1, then Enter.

0 Likes

#4

In addition, you can add your regrouped commands in the command palette with a common starting description:

{ "caption": "Code Folding: web_todo_excl", "command": "reg_replace", "args": {"replacements": "web_todo_excl"], "action": "fold"} }, { "caption": "Code Folding: web_log_filter", "command": "reg_replace", "args": {"replacements": "web_log_filter"], "action": "fold"} }
And add a keybinding that open the command palette with the same starting description, filtering everything else:

	{ "keys": "ctrl+alt+shift+f"], "command": "show_overlay", "args": {"overlay": "command_palette", "text": "Code Folding: "} },
0 Likes

#5

Thanks so much guys really apprechiate the tips… COD312 I was looking at key sequences in the documentation for version 1… I can’t find anything for v2… I’m still not exactly sure how this is supposed to work… could you elaborate a little more?

0 Likes

#6

Sure thing. Key sequences work exactly like regular keybindings. So you can place the following in your User Keybindings file: [pre=#2A2A2A]{
“keys”: “ctrl+j”,“p”],
“command”: “some_command”
}[/pre]

Then you trigger it by pressing control + j followed by p.

Important: there is a restriction on key sequences. You cannot assign a key sequence if the initial key already has a binding. For example. If you have { "keys": "ctrl+j"], "command": "some_command" }, you can’t have { "keys": "ctrl+j","p"], "command": "some_command" }

Which makes sense because sublime will trigger the keybinding first before a sequence can be created (if that makes any sense).

Let me know if you have any other questions.

0 Likes