Question

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!

Was it helpful?

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] } 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top