keyboard-shortcutssublimetext2rulers

How to create keyboard shortcut for displaying ruler in Sublime Text 2?


What I want is to toggle Sublime Text 2 rulers visibility with a keyboard shortcut.

The only information I have got on topic is that rulers can be controlled by following JSON code in Preferences:

{
"rulers": [80, 120],
}

Is it possible to create such a keyboard shortcut?

Thanks in advance!


Solution

  • You can add this in your user key bindings settings (menu Sublime Text 2/Preferences/Key Bindings - User):

    { 
      "keys": ["YOUR_ENABLE_RULERS_SHORTCUT"],
      "command": "set_setting",
      "args":
      {
        "setting": "rulers",
        "value": [80, 120]
      }
    }
    

    To disable rulers:

    { 
      "keys": ["YOUR_DISABLE_RULERS_SHORTCUT"],
      "command": "set_setting",
      "args":
      {
        "setting": "rulers",
        "value": []
      }
    }
    

    If you really want a toggle, you can create a new plugin (Tools/New Plugin...), with a code similar to this:

    import sublime, sublime_plugin
    
    class ToggleRulersCommand(sublime_plugin.TextCommand):
        def run(self, edit, **kwargs):
            rulers = kwargs["values"] if self.view.settings().get("rulers") == [] else []
            self.view.settings().set("rulers", rulers)
    

    Save the plug-in in your Packages/User directory, with name ToggleRulers.py.

    Then, add this key binding:

    { 
        "keys": ["YOUR_TOGGLE_RULERS_SHORTCUT"], "command": "toggle_rulers", 
        "args": { "values": [80, 120] } 
    }