Sublime Text 2: Trim trailing white space on demand

蹲街弑〆低调 提交于 2019-12-02 14:00:18
Sridhar Katakam

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" }

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
antou

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:

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

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" }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!