Home Download Buy Blog Forum Support

Keybinding draw_white_space toggle

Keybinding draw_white_space toggle

Postby DeerDance on Mon Mar 26, 2012 10:40 am

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...
DeerDance
 
Posts: 8
Joined: Mon Mar 26, 2012 10:37 am

Re: Keybinding draw_white_space toggle

Postby facelessuser on Mon Mar 26, 2012 9:07 pm

Code: Select all
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")


There you go.
facelessuser
 
Posts: 1565
Joined: Tue Apr 05, 2011 7:38 pm

Re: Keybinding draw_white_space toggle

Postby facelessuser on Tue Mar 27, 2012 3:05 am

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.

Code: Select all
    {
        "keys": ["f8"],
        "command": "toggle_white_space"
    }
facelessuser
 
Posts: 1565
Joined: Tue Apr 05, 2011 7:38 pm

Re: Keybinding draw_white_space toggle

Postby DeerDance on Wed Mar 28, 2012 9:45 pm

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...
DeerDance
 
Posts: 8
Joined: Mon Mar 26, 2012 10:37 am

Re: Keybinding draw_white_space toggle

Postby nick. on Wed Mar 28, 2012 10:24 pm

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.
nick.
 
Posts: 266
Joined: Wed Jan 18, 2012 3:45 am

Re: Keybinding draw_white_space toggle

Postby probiner on Mon Oct 08, 2012 10:37 pm

facelessuser wrote:
Code: Select all
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")


There you go.


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
probiner
 
Posts: 3
Joined: Mon Oct 08, 2012 6:26 pm

Re: Keybinding draw_white_space toggle

Postby facelessuser on Tue Oct 09, 2012 3:18 am

probiner wrote:
facelessuser wrote:
Code: Select all
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")


There you go.


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

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.
facelessuser
 
Posts: 1565
Joined: Tue Apr 05, 2011 7:38 pm

Re: Keybinding draw_white_space toggle

Postby Loreto on Mon Dec 24, 2012 2:22 pm

I just wrote a collection of toggle display functions:

Code: Select all
#!/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:

Code: Select all
    { "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
Loreto
 
Posts: 2
Joined: Mon Dec 24, 2012 2:11 pm

Re: Keybinding draw_white_space toggle

Postby FichteFoll on Sat Dec 29, 2012 3:01 am

There is this "toogle_setting" command which does the same, at least for booleans.
Code: Select all
{ "keys": ["f6"], "command": "toggle_setting", "args": {"setting": "spell_check"} },
FichteFoll
 
Posts: 386
Joined: Fri Mar 16, 2012 11:49 pm
Location: Germany

Re: Keybinding draw_white_space toggle

Postby Loreto on Tue Jan 01, 2013 12:35 pm

Good news.
For the boolean setting I changed my shortcut using your suggestion.
Thank You.
Loreto
 
Posts: 2
Joined: Mon Dec 24, 2012 2:11 pm


Return to Technical Support

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot] and 24 guests