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

前端 未结 8 1650
自闭症患者
自闭症患者 2020-11-29 18:06

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

相关标签:
8条回答
  • 2020-11-29 18:46

    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

    0 讨论(0)
  • 2020-11-29 18:47

    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.

    0 讨论(0)
  • 2020-11-29 18:47

    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, then 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.

    0 讨论(0)
  • 2020-11-29 19:04

    Take a look at this gist.

    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.

    0 讨论(0)
  • 2020-11-29 19:07

    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.

    0 讨论(0)
  • 2020-11-29 19:09

    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 image 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.

    0 讨论(0)
提交回复
热议问题