Home Download Buy Blog Forum Support

Multiple Key Bindings to One Key /w Dropdown?

Multiple Key Bindings to One Key /w Dropdown?

Postby raptor235 on Thu May 24, 2012 12:49 pm

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?
raptor235
 
Posts: 36
Joined: Mon Jan 16, 2012 7:13 pm

Re: Multiple Key Bindings to One Key /w Dropdown?

Postby C0D312 on Thu May 24, 2012 1:59 pm

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.
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: Multiple Key Bindings to One Key /w Dropdown?

Postby xeno.by on Thu May 24, 2012 2:28 pm

You can also do something like that:

Code: Select all
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.
xeno.by
 
Posts: 104
Joined: Mon Jan 30, 2012 10:03 am
Location: Lausanne, Switzerland

Re: Multiple Key Bindings to One Key /w Dropdown?

Postby bizoo on Thu May 24, 2012 2:33 pm

C0D312 wrote: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.

In addition, you can add your regrouped commands in the command palette with a common starting description:
Code: Select all
    {
       "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:
Code: Select all
   { "keys": ["ctrl+alt+shift+f"], "command": "show_overlay", "args": {"overlay": "command_palette", "text": "Code Folding: "} },
bizoo
 
Posts: 876
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Re: Multiple Key Bindings to One Key /w Dropdown?

Postby raptor235 on Fri May 25, 2012 2:30 am

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?
raptor235
 
Posts: 36
Joined: Mon Jan 16, 2012 7:13 pm

Re: Multiple Key Bindings to One Key /w Dropdown?

Postby C0D312 on Fri May 25, 2012 2:50 am

Sure thing. Key sequences work exactly like regular keybindings. So you can place the following in your User Keybindings file:
{
"keys": ["ctrl+j","p"],
"command": "some_command"
}


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
Code: Select all
{
    "keys": ["ctrl+j"],
    "command": "some_command"
}
, you can't have
Code: Select all
{
    "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.
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am


Return to Technical Support

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 6 guests