Sublime Forum

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

#1

Hello,
this key
{ "keys": ["alt+s"], "command": "toggle_setting", "args": {"setting": "draw_white_space"}}
hides SPACES/TABS characters in the active tab.

  1. I would like a shortcut to switch (hide then show then hide again, …) hiding and showing SPACES/TABS

  2. I would like this shortcut apply the setting globally: to all opened tabs and future opened tabs.

Solution from facelessuser works fine for my question 1, but not for my 2nd question.

Do you have any idea to solve question 2?
Thanks in advance and regards.

0 Likes

#2

My solution does it globally, what do you mean it doesn’t? It literally changes the global preference in the preference file.

import sublime
import sublime_plugin


class ToggleWhiteSpaceCommand(sublime_plugin.ApplicationCommand):
    """Toggle the showing of whitespace in Sublime."""

    def run(self):
        """Run the command."""

        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")
0 Likes

#3

Sorry facelessuser, it’s my mistake,
I didn’t test your solution deeply. Yes, it works fine and globally. I mean: when I use the shortcut to switch the setting (draw_white_space) in the active tab, the new setting’s value is applied to all other tabs too.
Thanks and regards.

0 Likes

#4

No problem. I was just confused.

0 Likes

#5

Sublime Tribesmen!
I’m new to customizing sublime other than .properties files.
I’ve searched the web for where to put the above Python code as a key binding but to no avail.
I don’t understand the code itself enough as well I think.
Is the name of the class ToggleWhiteSpaceCommand itself somehow interpreted? (a’la C++'s function mangling?) and that itself is the mechanism for substituting the action named draw_white_space.

How does one connect this code to the key binding?
Please help, I’ve 11 browser search tabs open I’m lost :-[

0 Likes

#6

You want to store the Python code in the top level of a package, typically your User package. You can use the Preferences > Browse Packages menu item or command palette entry to find that location if you don’t know where it is.

The command name is inferred from the name of the class used to define it. It gets converted into a snake_case version of the class name, and the _command suffix (if any) is thrown away. For the plugin above, that makes the name of the command toggle_white_space.

The videos linked below cover these topics in more details (the second one covers commands in general but the link takes you directly to the portion related to how the command name is created).

Additionally, the plugin above modifies your Preferences.sublime-settings file to swap the value of the draw_white_space setting. Since it modifies the preference directly, it takes effect globally in all files and all windows.

Just for the sake of completeness, the following plugin toggles any setting you like between any values you want, but it affects only the currently active file. That can sometimes be more desirable. The comments in the file have example key bindings for working with white space (this is actually the reason it was created in the first place).

1 Like