javifm
javifm

Reputation: 705

Trigger trailing white space on demand on Sublime text 3

Related with the third answer from this question, I would like to refactor this simple plugin so that it works with Sublime Text 3. Please, could you help me? I am new to python, and I don´t know anything about plugin development for sublime.

import sublime, sublime_plugin

def trim_trailing_white_space2(view):
    trailing_white_space2 = view.find_all("[\t ]+$")
    trailing_white_space2.reverse()
    edit = view.begin_edit()
    for r in trailing_white_space2:
        view.erase(edit, r)
    view.end_edit(edit)

class TrimTrailingWhiteSpace2Command(sublime_plugin.TextCommand):
    def run(self, edit):
        trim_trailing_white_space2(self.view)

I have searched and the problem is begin_edit() and end_edit(). I don´t want to install a plugin just for triggering trailing white space on demand. Thanks a lot, best regards.

Upvotes: 1

Views: 890

Answers (2)

Erasmus Cedernaes
Erasmus Cedernaes

Reputation: 1925

In Sublime Text 3 you simply need to map a shortcut for the native trim function. Just add

{ "keys": ["ctrl+alt+t"], "command": "trim_trailing_white_space" }

To your key bindings under PreferencesKey Bindings - User. It can then be executed by pressing ctrl+alt+t.

Upvotes: 1

javifm
javifm

Reputation: 705

Working on ST3.

import sublime, sublime_plugin

    class TrimTrailingWhiteSpace2Command(sublime_plugin.TextCommand):
        def run(self, edit):
            trailing_white_space = self.view.find_all("[\t ]+$")
            trailing_white_space.reverse()
            for r in trailing_white_space:
                self.view.erase(edit, r)

    class TrimTrailingWhiteSpace2(sublime_plugin.EventListener):
        def run(self, view):
            view.run_command("trim_trailing_white_space2")

Upvotes: 0

Related Questions