Sublime Forum

Toggle gutter shortcut

#1

Hello, how to make “Toggle Gutter” shortcut? I want to press, for example F5 or any other key, and to toogle gutter along with line numbers. I don’t know how to do that, please help.

0 Likes

#2

The toggle_setting command can turn a setting on and off, so you can create a key binding such as the following to do this; change the key as appropriate.

    {
        "keys": ["f5"],
        "command": "toggle_setting",
        "args": {"setting": "gutter"}
    },

The command will only affect the file you’re currently editing.

You can replace "gutter" with "line_numbers" if you want to keep the gutter but turn line numbers off; for example if you still want to see bookmarks you’ve set in the gutter without seeing the line numbers.

3 Likes

#3

The example given only toggles the gutter for the currently focused file. Is there a way to toggle gutter with hotkey, and have it take place application wide?

0 Likes

#4

There’s not a direct command that will toggle a setting globally as far as I’m aware; the toggle_setting command specifically operates on just the current view (as you’ve noticed). In order to make the setting toggle application wide, you would need a plugin of some sort.

This plugin implements a toggle_global_setting command you could use for this purpose. It’s based on this extended toggle_setting plugin and loads the global preferences file and toggles the setting there. Note however that this causes the the preferences setting to be rewritten (which may be annoying if you’ve got commented out items, for example).

import sublime
import sublime_plugin


class ToggleGlobalSettingCommand(sublime_plugin.ApplicationCommand):
    """
    An extended version of the toggle_setting internal command. Along with a
    setting, provide a list of options to toggle between, which can contain
    more than two items if the setting can have more than two values.
    (e.g. the draw_white_space option). The setting is altered in the global
    preferences file.
    """
    def run(self, setting, options):
        s = sublime.load_settings("Preferences.sublime-settings")
        try:
            current = s.get(setting)
            index = -1 if current is None else options.index(current)
        except:
            index = -1

        index = (index + 1) % len(options)
        s.set(setting, options[index])

        sublime.save_settings("Preferences.sublime-settings")

In use, you’d use it like the following (note that unlike the built in command, this extended command can toggle any setting between any value, so you need to tell it what the possible options are):

    {
       "keys": ["super+s"], "command": "toggle_global_setting",
       "args": {
          "setting": "gutter",
          "options": [true, false]
       }
    },

Doing this also has the side effect of every view having the same gutter state at all times (except for the case of syntax specific settings). If you’d rather literally flip the state of the the gutter for every buffer instead, that would require a plugin that uses toggle_setting once for every view in every window.

0 Likes