Search code examples
keyboard-shortcutssublimetext

Is it possible to chain key binding commands in sublime text 2?


There are times in Sublime Text when I want to reveal the current file in the side bar and then navigate around the folder structure.

This can be achieved using the commands reveal_in_side_bar and focus_side_bar however they have to be bound to two separate key combinations so I have to do 2 keyboard combinations to achieve my goal when ideally I'd like just one (I'm lazy).

Is there any way to bind multiple commands to a single key combination? e.g. something like this:

{
  "keys": ["alt+shift+l"], 
  "commands": ["reveal_in_side_bar", "focus_side_bar"]
},

Solution

Based on @artem-ivanyk's and @d_rail's answers

1) Tools → New Plugin

import sublime, sublime_plugin

class RevealInSideBarAndFocusCommand(sublime_plugin.WindowCommand):
    def run(self):
        self.window.run_command("reveal_in_side_bar")
        self.window.run_command("focus_side_bar")

Save as RevealInSideBarAndFocus.py

2) Sublime Text 2 → Preferences → Key Bindings — User

Bind it to shortcut:

{ "keys": ["alt+shift+l"], "command": "reveal_in_side_bar_and_focus" }

Solution

  • Updating @Artem Ivanyk's answer. I do not know what changed in Sublime, but that solution did not work for me, but I got this to work:

    import sublime, sublime_plugin
    
    class RevealInSideBarAndFocusCommand(sublime_plugin.WindowCommand):
        def run(self):
            self.window.run_command("reveal_in_side_bar")
            self.window.run_command("focus_side_bar")
    

    .

    { "keys": ["ctrl+shift+8"], "command": "reveal_in_side_bar_and_focus" }
    

    Btw, I'm using Build 2220