Sublime Text 2: Trim trailing white space on demand

前端 未结 5 637
时光取名叫无心
时光取名叫无心 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 18:50

    You can simply use a regex to remove trailing whitespaces:

    1. Find > Replace...
    2. Find what: [^\S\r\n]+$
    3. Replace with: leave empty.
    4. Click 'Replace All'

    [^\S\r\n]+$ is Regex for "at least one whitespace character (so spaces and tabs but not newlines, using a double negation) followed by the end of the line"

    Regular Expression must be enabled:

    0 讨论(0)
  • 2020-12-22 18:56

    Beware: using this plugin makes Sublime Text significantly slower

    I use TrailingSpaces plugin for this.

    Highlight trailing spaces and delete them in a flash.

    ST2 provides a way to automatically delete trailing spaces upon file save. Depending on your settings, it may be more handy to just highlight them and/or delete them by hand. This plugin provides just that!

    Usage: click "Edit / Trailing Spaces / Delete".

    To add a key binding, open "Preferences / Key Bindings - User" and add:

    { "keys": ["ctrl+alt+t"], "command": "delete_trailing_spaces" }
    
    0 讨论(0)
  • 2020-12-22 18:56

    I use these steps for a quick on-demand solution within Sublime Text:

    1. Find > Replace...
    2. Find What: [ \t]+\n
    3. Replace With: \n
    4. Replace All

    You could also do this for a large set of files via

    1. Find > Find in Files...
    2. Find: [ \t]+\n
    3. Where:
    4. Replace: \n
    5. Replace
    0 讨论(0)
  • 2020-12-22 19:00

    Here's a super simple way that uses no plugins or settings and works in most situations.

    1. Multi-Select and move cursor to the end of every line
    2. Hold CTRL-Shift, Press Left, Right
    3. The spaces and tabs at the end of the lines should now be selected. Press Delete or Backspace

      Note - Special characters such as ( and + may also be selected at the end of the line at this point, not just spaces.

    How to Multi-Select all lines:

    One way is to use the middle mouse key to select vertically then hit the End Key if it's a small selection.

    With hot-keys:

    1. CTRL-A (select all)
    2. CTRL-SHIFT-L (place cursor on all lines selected)
    3. END (Go to end of lines)

    You can also use the find function to find something that will be in every line, like the space character:

    1. \s (using regex)
    2. Click Find All
    3. Press the "End" key to get multiple cursors at the end of each line

    Sample Text:

    text and number     44  more text and a space  
    text and number 44  more text and 2 tabs        
    text and number 44  more text and no space or tab
    
    text and number 44  more text after a line feed
    
    0 讨论(0)
  • 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" }
    
    0 讨论(0)
提交回复
热议问题