Reputation: 71
Is there a dynamic way to enable and disable invisible white space display apart from persistent setting, via:
"draw_white_space": "all",
Upvotes: 5
Views: 2980
Reputation: 22791
The only way to control the state of the display for white space is via changing the setting that you reference in your question, draw_white_space
:
// Set to "none" to turn off drawing white space, "selection" to draw only the
// white space within the selection, and "all" to draw all white space
"draw_white_space": "selection",
For many such settings that are a Boolean value, you can bind a key to the toggle_setting
command, telling it what setting that you want to toggle between. However, for the case of the draw_white_space
option, it takes a string argument of either "all"
, "selection"
or "none"
and so the standard toggle command won't work.
The following is a simple plugin that implements a toggle_white_space
command which performs this operation for you. To use it, select Tools > Developer > New Plugin...
from the menu, replace the stub code you see with the plugin code here, and then save it as a .py
file in the location that Sublime will default to (your User
package):
import sublime
import sublime_plugin
class ToggleWhiteSpaceCommand(sublime_plugin.TextCommand):
def run(self, edit, options=["none", "selection", "all"]):
try:
current = self.view.settings().get("draw_white_space", "selection")
index = options.index(current)
except:
index = 0
index = (index + 1) % len(options)
self.view.settings().set("draw_white_space", options[index])
The defined command takes an optional argument named options
which allows you to specify the values that you want to toggle between, with the default being all of the possible options.
You can bind a key directly to the command to toggle the state of the setting between all of the possible values of the setting, or use something like the following if you only want to swap between it always being on and always being off, for example:
{
"keys": ["super+s"],
"command": "toggle_white_space",
"args": {
"options": ["all", "none"]
}
},
Note that although this is changing the setting, it's applying the setting directly to the currently focused view, which is persistent but only to the view in question and only as long as that view is open. This doesn't alter the setting for other files that are already open or for any new files that you might open in the future; the default for such files will still be what you have the setting set to in your user preferences.
Upvotes: 7