¿Es posible encadenar comandos de enlace de teclas en 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 y 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"]
},

Solución

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" }

preguntado el 10 de marzo de 12 a las 12:03

8 Respuestas

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.

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

respondido 31 mar '14, 14:03

I'm in Debian with Sublime 3 Build 3126. This plugin does not seem to work, advice pls. - T04435

Works like magic. Use it for commands with arguments just omit "args":. P.ej ["set_file_type", {"syntax": "PostgreSQL.tmLanguage"}] - Ovnis

A good solution for those not wanting to make their own plugins. - daniel dewhurst

sublimetext 3 it doesn't work, i tried with { "keys": ["ctrl+v"], "command": "chain", "args": { "commands": [ ["paste"], ["reindent"] ] } } - user924

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

respondido 02 nov., 12:05

working on v2.0.1 Build 2217. Please save the filename as RevealInSideBarAndFocus.py - KKK

I don't think file name matters, does it? - Geo

Not from my experience, but location and class name do. The "command" is the CamelCased python class converted to underscores. Also, save in Packages/CustomCommand carpeta, no en Packages/User. - d_carril

Stumbled upon similar problem. When trying to record macros, which involved „Save“ command, console threw at me „Unknown macros command save“ message. Worked my way around with elementary plugin.

1) Tools → New Plugin

import sublime, sublime_plugin

class MyChainedActionsCommand():
    def run(self):
        self.view.run_command("reveal_in_side_bar")
        self.view.run_command("focus_side_bar")

You need to use upper camel case notation for the class name. ST2 exposes this class for the command name with „Command“ suffix removed and the rest converted into the lowercase-underscore notation. I.e. in this example MyChainedActionsCommand could be run in sublime's console typing: view.run_command("my_chained_actions")

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

Bind it to shortcut:

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

Heed commas.

Respondido el 02 de junio de 12 a las 16:06

The code deos nothing in version 2.0.1 build 2217; No matter, whether it's MyChainedActionsCommand() or MyChainedActionsCommand(sublime_plugin.WindowCommand) o lo que sea. - mreq

Echa un vistazo a esta esencia.

I've been trying to implement this in a long time and found this by accident.

Don't forget to read the "documentation" provided. I kept trying to make this work, until I reallized I was not passing the "context" key.

Respondido 08 Feb 13, 07:02

This gist is awesome. Thank you for sharing the link. +1 Ps.: I actually didn't used context, I simply omitted it and my key bind worked. - RafaelDDL

You can create a macro to do this. For Sublime Text, macros are essentially just chained commands. You then create a keybinding for that macro. You can create a macro by using Tools > Record Macro, then executing your commands (beware that macros record keystrokes as well, so you'll want to use the commands from the menu bar to not cause conflicts), then Stop Recording, entonces Save Macro. After you save the macro, you can open it back up in Sublime Text to make sure that it recorded only what you want.

respondido 10 mar '12, 13:03

I've set up a macro, however it's saying "Unknown macro command reveal_in_side_bar" in the console (Says the same fir focus_side_bar). Is there a way to reference existing commands from inside a macro? - James Hollingworth

Hmm. ST2's macros seem to be picky as to what they will and will not record/accept as valid commands. If that's the case, you'll have to make a plugin that chains these commands using run_command(). It's not as complicated as it sounds. I'll update my answer shortly. - Liam Caín

sobre la base de Artem Ivanyk reply, here is a version of ChainedActions that works with arguments. It takes two arguments for actions y args. Both are lists and each command in the list gets executed with the corresponding arguments. This admittedly stupid example inserts two snippets: view.run_command("chained_actions", {"actions":["insert_snippet","insert_snippet"],"args":[{"contents": "($0)"},{"contents": "1($0)"}]})`

import sublime
import sublime_plugin

class ChainedActionsCommand(sublime_plugin.TextCommand):
    def run(self, edit, actions, args):
        for i, action in enumerate(actions):
            self.view.run_command(action, args[i])

contestado el 23 de mayo de 17 a las 12:05

I've tried to use the same command but I ended up with a bug that when the file's folder was already unfolded sublime moved my focus sidebar's top, where I can see the open files. To improve this behavior I've wrote a new plugin that ensures it'll behave as I want to, here it is https://github.com/miguelgraz/FocusFileOnSidebar

Respondido el 11 de junio de 14 a las 18:06

I am using Sublime text3 build - 3083. It solves the problem just by 'Reveal it in side bar', the focus comes automatically.

I have added a custom keyboard shortcut for 'Reveal in sidebar' by adding the following statement under Preferences->Key Bindings-User :

[
    { "keys": ["ctrl+shift+r"], "command": "reveal_in_side_bar"}
]

The option - 'Reveal in sidebar' was missing for imagen file types, since the context menu doesn't appear with the right click of the mouse. The custom keyboard shortcut comes handy in this situation.

Respondido 08 Jul 15, 09:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.