Sublime Forum

Keybinding draw_white_space toggle

#1

How would I go about setting F8 key to toggle between “draw_white_space” : “all” and “draw_white_space” : “selection”

I tried several things and gaze at default ones to figure out pattern but now I rather ask and wait…

0 Likes

#2

[code]import sublime
import sublime_plugin

class ToggleWhiteSpaceCommand(sublime_plugin.ApplicationCommand):
def run(self):
settings = sublime.load_settings(“Preferences.sublime-settings”)
white_space = “selection” if settings.get(“draw_white_space”, “selection”) != “selection” else “all”
settings.set(“draw_white_space”, white_space)
sublime.save_settings(“Preferences.sublime-settings”)[/code]

There you go.

0 Likes

One shortcut to switch between 2 states (globaly) for draw_white_space
#3

Forgot the rest of what you were asking.

Bind the f8 key to the new plugins command. Put this in your user key map file.

{ "keys": "f8"], "command": "toggle_white_space" }

0 Likes

#4

Thank you very much.
Works like a charm, though I was little disappointed that it required coding a plugin, I imagined that I am learning slowly custom keybinds and BAM, neck deep in unknown.

Also I am a moron who could just ctrl+a anytime whitespace is needed, though this is much elegant…

0 Likes

#5

Plugin is a bit of an unfair name for it, since most people (me anyway) assume plugins to provide large-scale additional functionality. Sublime plugins can be that (see Package Control, Code Intel, Bracket Highlighter) or they can be extremely small (but useful!). They’re technically the same, just some do more so the term ‘plugin’ feels more apt.

0 Likes

#6

[quote=“facelessuser”][code]import sublime
import sublime_plugin

class ToggleWhiteSpaceCommand(sublime_plugin.ApplicationCommand):
def run(self):
settings = sublime.load_settings(“Preferences.sublime-settings”)
white_space = “selection” if settings.get(“draw_white_space”, “selection”) != “selection” else “all”
settings.set(“draw_white_space”, white_space)
sublime.save_settings(“Preferences.sublime-settings”)[/code]

There you go.[/quote]

Sorry for the newbiness. But how do I apply this? Tried to paste it on the console but a “IndentationError: unexpected indent” shows up.

Cheers

0 Likes

#7

[quote=“probiner”]

[quote=“facelessuser”][code]import sublime
import sublime_plugin

class ToggleWhiteSpaceCommand(sublime_plugin.ApplicationCommand):
def run(self):
settings = sublime.load_settings(“Preferences.sublime-settings”)
white_space = “selection” if settings.get(“draw_white_space”, “selection”) != “selection” else “all”
settings.set(“draw_white_space”, white_space)
sublime.save_settings(“Preferences.sublime-settings”)[/code]

There you go.[/quote]

Sorry for the newbiness. But how do I apply this? Tried to paste it on the console but a “IndentationError: unexpected indent” shows up.

Cheers[/quote]

Save it in a file called something like draw_white_space_toggle.py. Drop it in your User folder under Packages. Add the keybinding I showed in a later post to your keybinding file under User (you can go to Preferences->Key Bindings-User in the menu. That will create the file if it doesn’t already exist).

After that, simply press the key you defined and watch the white space get toggled.

0 Likes

#8

I just wrote a collection of toggle display functions:

#!/usr/bin/python -O
# -*- coding: iso-8859-15 -*-

import sublime, sublime_plugin

class LnToggleDisplayCommand(sublime_plugin.TextCommand):
    def run(self, edit, **kwargs):
        action = kwargs'action'].upper()
        view = self.view
        my_id = self.view.id()

            # --------------------------------------------------------------
            # - To see what settings are available, and a description of each,
            # - take a look at Packages/Default/Preferences.sublime-settings.
            # --------------------------------------------------------------
        settings = view.settings()

        if action == 'WHITE_SPACE':
            propertyName, propertyValue1, propertyValue2 = "draw_white_space", "all", "selection"

        elif action == 'GUTTER':
            propertyName, propertyValue1, propertyValue2 = "gutter", False, True

        elif action == 'LINE_NO':
            propertyName, propertyValue1, propertyValue2 = "line_numbers", False, True
            # propertyName = "line_numbers"

        elif action == 'INDENT_GUIDE':
            propertyName, propertyValue1, propertyValue2 = "draw_indent_guides", False, True

        else:
            propertyValue   = None


        if propertyName:
            propertyValue = propertyValue1 if settings.get(propertyName, propertyValue1) != propertyValue1 else propertyValue2
            settings.set(propertyName, propertyValue)

copy the code and save it to a file into your /Packages/yourPlugin/toggleDisplay.py

Associate the displays to the relative keyboard shortcuts:

    { "keys": "alt+d", "alt+b"], "command": "ln_toggle_display", "args": {"action": "WHITE_SPACE"}},
    { "keys": "alt+d", "alt+l"], "command": "ln_toggle_display", "args": {"action": "LINE_NO"}},
    { "keys": "alt+d", "alt+i"], "command": "ln_toggle_display", "args": {"action": "INDENT_GUIDE"}},
    { "keys": "alt+d", "alt+g"], "command": "ln_toggle_display", "args": {"action": "GUTTER"}},

I hope it can be useful for someone.
Ciao

0 Likes

#9

There is this “toogle_setting” command which does the same, at least for booleans.

{ "keys": "f6"], "command": "toggle_setting", "args": {"setting": "spell_check"} },
0 Likes

#10

Good news.
For the boolean setting I changed my shortcut using your suggestion.
Thank You.

0 Likes