Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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" } 
like image 566
James Hollingworth Avatar asked Mar 10 '12 12:03

James Hollingworth


2 Answers

Although the question is a year old, this might help people that are still looking for an answer.

Recently, a new package was developed by jisaacks, called Chain of command. It has the primary task to do exactly what you request, to chain several commands at once.

The package can be found here: https://github.com/jisaacks/ChainOfCommand

An example of the working can be found below.

Let's say you wanted a key binding to duplicate the current file. You could set this key binding:

{   "keys": ["super+shift+option+d"],    "command": "chain",    "args": {     "commands": [       ["select_all"],       ["copy"],       ["new_file"],       ["paste"],       ["save"]     ]   } } 

This would select all the text, copy it, create a new file, paste the text, then open the save file dialog.

Source: https://sublime.wbond.net/packages/Chain%20of%20Command.

like image 91
Winfried Avatar answered Sep 22 '22 11:09

Winfried


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

like image 40
d_rail Avatar answered Sep 19 '22 11:09

d_rail