Sublime Text 2: Trim trailing white space on demand

前端 未结 5 646
时光取名叫无心
时光取名叫无心 2020-12-22 18:45

I know that Sublime Text 2 can delete the trailing white space on files upon saving.

When working in a team and commiting a change to a file this tends to produce hu

5条回答
  •  醉话见心
    2020-12-22 19:15

    I found a soulution here: http://www.sublimetext.com/forum/viewtopic.php?f=4&t=4958

    You can modify the package

    trim_trailing_white_space.py
    

    located in the default packages directory, this way:

    import sublime, sublime_plugin
    
    def trim_trailing_white_space(view):
        trailing_white_space = view.find_all("[\t ]+$")
        trailing_white_space.reverse()
        edit = view.begin_edit()
        for r in trailing_white_space:
            view.erase(edit, r)
        view.end_edit(edit)
    
    class TrimTrailingWhiteSpaceCommand(sublime_plugin.TextCommand):
        def run(self, edit):
            trim_trailing_white_space(self.view)
    
    class TrimTrailingWhiteSpace(sublime_plugin.EventListener):
        def on_pre_save(self, view):
            if view.settings().get("trim_trailing_white_space_on_save") == True:
                trim_trailing_white_space(view)
    
    class EnsureNewlineAtEof(sublime_plugin.EventListener):
        def on_pre_save(self, view):
            if view.settings().get("ensure_newline_at_eof_on_save") == True:
                if view.size() > 0 and view.substr(view.size() - 1) != '\n':
                    edit = view.begin_edit()
                    view.insert(edit, view.size(), "\n")
                    view.end_edit(edit)
    

    Now you can add the command to your keymap configuration:

    { "keys": ["your_shortcut"], "command": "trim_trailing_white_space" }
    

提交回复
热议问题