1

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.

Community
  • 1
  • 1
javifm
  • 705
  • 4
  • 9
  • 20
  • 1
    Sublime has this built in, you can just set `"trim_trailing_white_space_on_save": true,` in settings. – Kos Feb 24 '15 at 14:56
  • Yes I know, but I work in a team with people who does not trim trailing white space, so I just want to remove white spaces on my own files. Triggering from a shortcut is perfect. – javifm Feb 24 '15 at 14:59
  • 4
    If your project doesn't have this convention, I suggest to either enforce it and have everyone set up their editor, or just forget it and not clutter the diffs with unrelated whitespace changes in "your own files". Consistency wins here. – Kos Feb 24 '15 at 15:06

2 Answers2

2

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.

Erasmus Cedernaes
  • 1,787
  • 19
  • 15
0

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")
javifm
  • 705
  • 4
  • 9
  • 20