Highlight trailing spaces and delete them in a flash.

Overview

Trailing Spaces

A Sublime Text plugin that allows you to…

highlight trailing spaces and delete them in a flash!


Synopsis

Sublime Text provides a way to automate deletion of trailing spaces upon file saving (more on this at the end of this file). Depending on your settings, it may be more handy to just highlight them and/or delete them by hand, at any time. This plugin provides just that, and a lot of options to fine-tune the way you want to decimate trailing spaces.

Installation

It is available through Sublime Package Control and this is the recommended way of installation (brings configuration instructions, automatic updates with changelogs…).

Alternative installation methods

From github

You can install from github if you want, although Package Control automates just that. Go to your Packages directory (find out where it is by running Preferences: Browse Packages from The Command Palette) and clone this repository:

git clone https://github.com/SublimeText/TrailingSpaces.git

Manually

Download the plugin as a zip. Copy the Trailing Spaces directory to its location (see prior section).

Usage

Deletion

The main feature you gain from using this plugin is that of deleting all trailing spaces in the currently edited document. In order to use this deletion feature, you may either:

  • click on "Edit / Trailing Spaces / Delete";
  • bind the deletion command to a keyboard shortcut:

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

{ "keys": ["ctrl+shift+t"], "command": "delete_trailing_spaces" }

With this setting, pressing Ctrl + Shift + t will delete all trailing spaces at once in the current file! For OSX users, quoting wbond: "When porting a key binding across OSes, it is common for the ctrl key on Windows and Linux to be swapped out for super on OS X" (eg. use "super+shift+t" instead).

Beware: the binding from this example overrides the default ST's mapping for reopening last closed file. You can look at the default bindings in "Preferences / Key Bindings - Default".

Toggling highlighting

At any time, you can toggle highlighting on and off. You may either:

  • click on "Edit / Trailing Spaces / Highlight Regions"
  • bind the toggling command to a keyboard shortcut:
// I like "d", as in "detect" (overrides a default binding, though).
{ "keys": ["ctrl+shift+d"], "command": "toggle_trailing_spaces" }

Options

Several options are available to customize the plugin's behavior. Those settings are stored in a configuration file, as JSON. You must use a specific file: Go to "Preferences / Package Settings / Trailing Spaces / Settings" to add you custom settings.

A few of them are also accessible through the "Edit / Trailing Spaces" menu. Sometimes, editing a setting will require a fresh Sublime Text to be applied properly, so try relaunching ST before reporting an issue ;)

All settings are global (ie. applied to all opened documents).

Changing the highlighting color

Default: "invalid"

You may change the highlighting color, providing a color scope name such as "error", "comment"… just like that:

{ "highlight_color": "comment" }

The scope should be defined in your current theme file. Here is a dummy, fully-fledged example (feel free to cut irrelevant pieces for your settings) of such a custom color scope:

<dict>
  <key>name</key>
  <string>Invalid - Illegal</string>
  <key>scope</key>
  <string>invalid.illegal</string>
  <key>settings</key>
  <dict>
    <key>background</key>
    <string>#F93232</string>
    <key>fontStyle</key>
    <string></string>
    <key>foreground</key>
    <string>#F9F2CE</string>
  </dict>
</dict>

You would then use the value of "invalid.illegal".

Keeping trailing spaces invisible

You can make trailing spaces "invisible" yet still rely on the deletion command. To do that, set the highlight scope to an empty string:

{ "highlight_color": "" }

Beware: this is not the same as disabling the highlighting (see "On- Demand Matching" below). With this setting, the plugin still runs when opening a file, and in the background afterwards; you just won't see the trailing spaces (they are being highlighted with a "transparent" color).

Include Current Line

Default: true

Highlighting of trailing spaces in the currently edited line can be annoying: each time you are about to start a new word, the space you type is matched as a trailing spaces. Currently edited line can thus be ignored:

{ "include_current_line": false }

Even though the trailing spaces are not highlighted on this line, they are still internally matched and will be delete when firing the deletion command.

Include Empty Lines

Default: true

When firing the deletion command, empty lines are matched as trailing regions, and end up being deleted. You can specifically ignore them:

{ "include_empty_lines": false }

They will not be highlighted either.

Modified Lines Only

Default: false (reopen ST to update)

When firing the deletion command, trailing regions in the entire document are deleted. There are some use-cases when deleting trailing spaces only on lines you edited is smarter; for instance when commiting changes to some third-party source code.

At any time, you can change which area is covered when deleting trailing regions. You may either:

  • click on "Edit / Trailing Spaces / Modified Lines Only"
  • specify as a setting:
{ "modified_lines_only": true }

There is also a command to toggle this feature on and off. You may thus define a key binding:

{ "keys": ["pick+a+shortcut"], "command": "toggle_trailing_spaces_modified_lines_only" }

Trim On Save

Default: false

Setting this to true will ensure trailing spaces are deleted when you save your document. It abides by the other settings, such as Modified Lines Only.

{ "trim_on_save": true }

Save After Trim

Default: false

You may not want to always trim trailing spaces on save, but the other way around could prove useful. Setting this to true will automatically save your document after you fire the deletion command:

{ "save_after_trim": true }

It is obviously ignored if Trim On Save is on.

Live Matching vs On-demand Matching

Default: true (reopen ST to update)

By default, trailing regions are matched every time you edit the document, and when you open it.

This feature is entirely optional and you may set it off: firing the deletion command will cause the trailing spaces to be deleted as expected even though they were not matched prior to your request. If you are afraid of the plugin to cause slowness (for instance, you already installed several heavy plugins), you can disable live matching:

{ "enabled": false }

In this case, for no trailing regions are matched until you request them to be deleted, no highlighting occurs—it is in fact disabled, regardless of your "scope" setting. If you want to check the trailing spaces regions, you can toggle highlighting on and off. In this case, it may come in handy to define a binding for the toggling command. When "On-demand Matching" is on and some trailing spaces are highlighted, added ones will obviously not be. Toggling highlight off and on will refresh them.

Ignore Scope

Default: ["text.find-in-files", "source.build_output", "source.diff", "text.html.markdown"]

With this option you can ignore lines being highlighted based on the scope of their trailing region.

If at least one scope in the configured list matches a scope in the trailing region of the line, it won't be highlighted.

By default, the scope under the mouse cursor is shown by pressing Option+Command+P (OS X) or Ctrl+Alt+Shift+P (Windows, Linux)

// Trailing spaces for Find Results, Build output, Diff and Markdown are ignored
{ "scope_ignore": ["text.find-in-files", "source.build_output", "source.diff", "text.html.markdown"] }

For power-users only!

Disabled for large files

The plugin is disabled altogether for large files, for it may cause slowness. The default threshold is around 1 million of characters. This is configurable (in "File Settings - User") and the unit is number of chars:

{ "file_max_size": 1000}

The matching pattern

Default: [ \t]+

Trailing spaces are line-ending regions containing at least one simple space, tabs, or both. This pattern should be all you ever need, but if you do want to abide by another definition to cover edge-cases, go ahead:

// *danger* will match newline chars and many other folks
"regexp": "[\\s]+"

About Sublime Text's built-in features

Trailing Spaces is designed to be a drop-in replacement of the limited Trim Whitespace On Save built-in feature. ST is indeed able to delete trailing spaces upon saving files, and maybe that's all you need!

In order to enable this behavior, edit "Preferences / Settings" to add the following:

{ "trim_trailing_white_space_on_save": true }

As Trailing Spaces bypasses this setting, you will have to uninstall it to benefit from this setting.

Made a little less obvious in the documentation are settings to showcase whitespaces (not only trailing ones!):

{ "draw_white_space": "all" }

and to ensure a newline is kept at end of file upon saving:

{ "ensure_newline_at_eof_on_save": true }

The former will display all whitespaces in your files. There is another value of "selection" which display whitespaces under (you got it) your current text selection.

Comments
  • simply does not work?

    simply does not work?

    I install this plugin. (I am on Windows). I choose the Edit / Trailing Spaces / Delete command.

    Nothing seems to happen. Trailing spaces are not in fact deleted. Although I don't see any visible error message.

    Is there anything I can easily do to provide more debugging info? (I do not have experience developing ST plugins).

    Is this plugin still in fact supported/maintained by anyone?

    opened by jrochkind 18
  • Doesn't seem to work with ST2

    Doesn't seem to work with ST2

    Hey, installed via Package Control and I don't see any highlighting showing up (nor does the delete trailing spaces command do anything - "no trailing spaces found").

    opened by ghost 17
  • Ignore views based on the syntax used.

    Ignore views based on the syntax used.

    TrailingSpaces is ignored based on the syntax used in the view.

    { "trailing_spaces_syntax_ignore": ["Diff"] } ignores all views that use a syntax containing the string "Diff" like the Git diff view.

    opened by ulfsauer0815 17
  • Broken in ST3

    Broken in ST3

    Noticed this while installing the plugin on ST3:

    Traceback (most recent call last): File "C:\MJ\Programs\Windows\Sublime Text 3\sublime_plugin.py", line 286, in on_selection_modified callback.on_selection_modified(v) File "C:\MJ\Programs\Windows\Sublime Text 3\Data\Packages\TrailingSpaces\trailing_spaces.py", line 392, in on_selection_modified match_trailing_spaces(view) File "C:\MJ\Programs\Windows\Sublime Text 3\Data\Packages\TrailingSpaces\trailing_spaces.py", line 123, in match_trailing_spaces (matched, highlightable) = find_trailing_spaces(view) File "C:\MJ\Programs\Windows\Sublime Text 3\Data\Packages\TrailingSpaces\trailing_spaces.py", line 90, in find_trailing_spaces regexp = ts_settings.get("trailing_spaces_regexp") + "$" TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

    Simple fix would be to change line 90 to

    regexp = ts_settings.get("trailing_spaces_regexp", "") + "$"
    
    bug 
    opened by maheshjag 17
  • Can't turn highlighting off. Plus I hate pink. = Rather annoying.

    Can't turn highlighting off. Plus I hate pink. = Rather annoying.

    No matter what I do, I just can't "toggle" trailing space highlighting off. So now every document I work on is peppered with bright pink - even in places where I'm happy to let sleeping whitespace lie..

    A few things I've tried:

    • "Toggling" trailing spaces off
    • Toggling trailing spaces on, then off, then on, then off again
    • Closing the current file and opening a different one
    • Closing that different file then opening the original (yes - pink still there)
    • Closing an entire project then reopening it
    • Closing an entire project then toggling trailing spaces off, then reopening the project
    • Closing an entire project then toggling trailing spaces on, then off, then reopening the project
    • Restarting Sublime (yes - still pink)

    This is where it gets good:

    • Disabling the plugin completely (pink still there)
    • Closing all files and projects, enabling the plugin, then disabling it, then opening a file (still there)
    • Removing the plugin completely, and visualising flushing it down a toilet (still there)
    • Punching my computer screen (oh yes - PINK STILL THERE)

    So a little perplexed as you can imagine. Not to mention a little pissed.

    Anything I can do to remove this god-forsaken pink from my sights? Or am I doomed forever to have every Sublime document I ever work on lightly sprinkled with bright pink splodges?

    opened by ar1403 15
  • High CPU usage when this plugin is enabled

    High CPU usage when this plugin is enabled

    I see a high CPU usage (around 15-20%) with TrailingSpaces enabled, even when sublime is on background. My operating system is Arch Linux, sublime-text-dev package...

    opened by xhpohanka 13
  • Speedup matching

    Speedup matching

    Currently live matching will always search through the whole file which can be very slow for large files and more complex regex patterns.

    With this change it will only search through the currently visible region of text.

    opened by mheinzler 11
  • Setting a different color scope

    Setting a different color scope

    I realize you're busy. Hopefully this is an easy fix.

    I'm using the Soda theme, and I'm using Espresso Soda.tmTheme. In that file, there is the following defintion:

        <dict>
            <key>name</key>
            <string>Invalid - Illegal</string>
            <key>scope</key>
            <string>invalid.illegal</string>
            <key>settings</key>
            <dict>
                <key>background</key>
                <string>#F93232</string>
                <key>fontStyle</key>
                <string></string>
                <key>foreground</key>
                <string>#F9F2CE</string>
            </dict>
        </dict>
    

    In my user preferences, I have set:

    {   
        ...
        "trailing_spaces_highlight_color": "invalid.illegal",
        ...
    }
    

    I'm getting an odd color for trailing spaces that doesn't change, regardless of whether I set the above setting, and regardless of what I set it to. The color I get is like the inverse of Monokai: a pink-bordered black box.

    It's pretty jarring with this color scheme, so if I'm doing something wrong, I'd like to fix it.

    opened by jrhorn424 11
  • Option Request: Ignore indentation

    Option Request: Ignore indentation

    Could we please have a configurable option to ignore unused indentation? Meaning any white space between newlines with no other text. I like to keep lines between code blocks at the same indentation level as the lines above and below it, and the plugin really hates that.

    Example:
    Screen Shot 2012-12-11 at 11 22 01 AM

    feature request 
    opened by Twipped 10
  • Clean up settings

    Clean up settings

    • separate settings into its own class
    • apart from trailing_spaces_highlight_color which has special handling, get all options directly from settings so that they are always up to date with user preferences. Previously settings were only read on init.
    opened by rchl 9
  • automatically clean up lines that I type, but not others

    automatically clean up lines that I type, but not others

    If I'm editing a file, I don't want to add new trailing whitespace, but I also don't want to be submitting massive diffs that clean up all the whitespace in a big file while I'm making an unrelated change.

    So when I type "return return ", I want to automatically clean up the whitespace that's left behind, but I don't want to use the "nuclear option" of "delete trailing spaces", since that's going to affect lots of lines I didn't write.

    A good start on this would be for "delete trailing spaces" to only work on selected lines, if there's an active selection.

    feature request 
    opened by glyph 9
  • trim_trailing_white_space_on_save:true invalidates undo action (ctrl+y)

    trim_trailing_white_space_on_save:true invalidates undo action (ctrl+y)

    Using trim_trailing_white_space_on_save:true will invalidate the redo action (CTRL + Y) after saving.

    Reported here and here.

    Can cause some people to lose big amount of code when doing multiple ctrl+z.

    opened by alvarotrigo 0
  • Should not trim current line on save given these settings

    Should not trim current line on save given these settings

    My current user settings are:

    {
        "trailing_spaces_trim_on_save": true,
        "trailing_spaces_include_current_line" : false
    }
    

    I believe that this means that my current line should not be trimmed when I hit save, but it does get trimmed.

    opened by pavellishin 2
  • Automatic trailing white spaces on line move

    Automatic trailing white spaces on line move

    So if I'm on a line of code and I move to another line the trailing spaces are automatically deleted from last line I was on.

    Pros:

    1. When on an untitled tab where let's say I paste some code and edit it I'll be able to delete trailing spaces by moving from line to line (As of right now I can't delete trailing spaces on save since it's an untitled tab, so I have to call Delete Trailing spaces)
    2. No need to call Delete Trailing Spaces since it'll just delete all the trailing spaces when I move from line to line. (Yet you'll keep this simply because of people just installing this, and call this on files which they already have trailing spaces)
    3. Your algorithm wouldn't have to search the whole file for trailing spaces, again since it'll do it by moving from line to line.

    Cons:

    I had one in mind but I already forgot so I'm not sure if there really is any.

    So over all this would improve productivity because we don't need to wait till we save file, or call Delete Trailing Spaces

    If this is a sure no, perhaps as an option in the settings

    opened by eorroe 3
  • TrailingSpaces always trims on save although it's set to false

    TrailingSpaces always trims on save although it's set to false

    I'm on Linux build ST3 3065 and although I kept the default settings

    {
        "trailing_spaces_modified_lines_only": false,
        "trailing_spaces_trim_on_save": false,
    }
    

    the plugin always deletes all whitespaces in my file when I save it.

    opened by Heart1010 7
  • Command to force to delete all trailing spaces

    Command to force to delete all trailing spaces

    Now I set "trailing_spaces_modified_lines_only" to true in the configuration, this feature is really useful to me. But sometimes I really need to delete all trailing spaces in the whole file, not only modified lines, but after I use command to delete trailing spaces, some spaces are not removed. So I guess the problem is "trailing_spaces_modified_lines_only". Is there any way that I can force to delete trailing spaces in the whole file? Thanks.

    opened by bachue 0
Releases(v1.3.5)
  • v1.3.5(Jun 28, 2021)

  • v1.3.4(Feb 25, 2021)

  • v1.3.3(May 15, 2020)

  • v1.3.2(Apr 27, 2020)

    Maintenance / Improvements

    • Remove trailing_spaces_ prefixes from settings. Settings with prefixes are still respected for backward-compatibility.
    • Use sublime_lib.NamedSettingsDict to handle settings
    • Apart from trailing_spaces_highlight_color which has special handling, get all options directly from settings so that they are always up-to-date with user preferences. Previously settings were only read on init.
    Source code(tar.gz)
    Source code(zip)
  • v1.3.1(Apr 12, 2020)

    Improvements

    • Run async versions of all listeners (#139) (makes sure that main thread is never blocked) (ea3ff9842cf65c811dd675d49107e4db47820d09)
    Source code(tar.gz)
    Source code(zip)
  • v1.3.0(Apr 10, 2020)

    Features

    • New config option: trailing_spaces_scope_ignore (thx @smanolloff) (6bf6b8801622a990f54cf336605e4d7290c573df) Defaults to ignoring find-in-files, diff and build output views.

    Fixes

    • Don't leave file handle open after opening file for reading (with trim_modified_lines_only enabled) (0c58ab22a7e75d2d92e3a63b441c03f631d6213b)
    • Ignore trailing spaces on all lines when making edits with multiple cursors and trailing_spaces_include_empty_lines: false (#137) (299333dc3b7cf469c8e00439ccbe4890be83017f)

    Improvements

    • Major improvements to performance (tnx @mheinzler) (ad9096d6b499519b3bae07aab725c926d2cf3592)
    • Use region.redish to highlight trailing spaces (#133) (86b0843fa1aab6981a1155f33aff5f32d8cb8fef) This results in trailing spaces being consistently "redish", regardless of theme used
    • Ignore scratch views (#134) (e24719053fe6960a327591d2c48ccbd49684500d)
    • Ignore widgets (#138) (955ccaf4257014797fdbdb8943ccd54278a81043)
    Source code(tar.gz)
    Source code(zip)
  • v1.2.0(Mar 23, 2020)

    Features:

    • Add palette command to open preferences
    • Open settings in split view

    Fixes:

    • Fixed crash in files without selection (#123)

    Chore:

    • Add lint configuration and fix minor style issues
    Source code(tar.gz)
    Source code(zip)
Owner
Sublime Text Packages
Collection of Sublime Text packages maintained by the community. Come join us on Discord!
Sublime Text Packages
The Universal Device Detection library will parse any User Agent and detect the browser, operating system, device used (desktop, tablet, mobile, tv, cars, console, etc.), brand and model.

DeviceDetector Code Status Description The Universal Device Detection library that parses User Agents and detects devices (desktop, tablet, mobile, tv

Matomo Analytics 2.4k Jan 5, 2023
Extensive, portable and performant handling of UTF-8 and grapheme clusters for PHP

Patchwork UTF-8 for PHP Patchwork UTF-8 gives PHP developpers extensive, portable and performant handling of UTF-8 and grapheme clusters. It provides

Nicolas Grekas 80 Sep 28, 2022
Provides an object-oriented API to strings and deals with bytes, UTF-8 code points and grapheme clusters in a unified way.

String Component The String component provides an object-oriented API to strings and deals with bytes, UTF-8 code points and grapheme clusters in a un

Symfony 1.3k Dec 29, 2022
php-crossplane - Reliable and fast NGINX configuration file parser and builder

php-crossplane Reliable and fast NGINX configuration file parser and builder ℹ️ This is a PHP port of the Nginx Python crossplane package which can be

null 19 Jun 30, 2022
A tiny PHP class-based program to analyze an input file and extract all of that words and detect how many times every word is repeated

A tiny PHP class-based program to analyze an input file and extract all of that words and detect how many times every word is repeated

Max Base 4 Feb 22, 2022
PHP library to detect and manipulate indentation of strings and files

indentation PHP library to detect and manipulate the indentation of files and strings Installation composer require --dev colinodell/indentation Usage

Colin O'Dell 34 Nov 28, 2022
A PHP class which allows the decoding and encoding of a wider variety of characters compared to the standard htmlentities and html_entity_decode functions.

The ability to encode and decode a certain set of characters called 'Html Entities' has existed since PHP4. Amongst the vast number of functions built into PHP, there are 4 nearly identical functions that are used to encode and decode html entities; despite their similarities, however, 2 of them do provide additional capabilities not available to the others.

Gavin G Gordon (Markowski) 2 Nov 12, 2022
A lightweight php class for formatting sql statements. Handles automatic indentation and syntax highlighting.

SqlFormatter A lightweight php class for formatting sql statements. It can automatically indent and add line breaks in addition to syntax highlighting

Jeremy Dorn 3.9k Jan 1, 2023
Converts a string to a slug. Includes integrations for Symfony, Silex, Laravel, Zend Framework 2, Twig, Nette and Latte.

cocur/slugify Converts a string into a slug. Developed by Florian Eckerstorfer in Vienna, Europe with the help of many great contributors. Features Re

Cocur 2.8k Dec 22, 2022
A fast PHP slug generator and transliteration library that converts non-ascii characters for use in URLs.

URLify for PHP A fast PHP slug generator and transliteration library, started as a PHP port of URLify.js from the Django project. Handles symbols from

Aband*nthecar 667 Dec 20, 2022
ColorJizz is a PHP library for manipulating and converting colors.

#Getting started: ColorJizz-PHP uses the PSR-0 standards for namespaces, so there should be no trouble using with frameworks like Symfony 2. ###Autolo

Mikeemoo 281 Nov 25, 2022
Library for free use Google Translator. With attempts connecting on failure and array support.

GoogleTranslateForFree Packagist: https://packagist.org/packages/dejurin/php-google-translate-for-free Library for free use Google Translator. With at

Yurii De 122 Dec 23, 2022
:clamp: HtmlMin: HTML Compressor and Minifier via PHP

??️ HtmlMin: HTML Compressor and Minifier for PHP Description HtmlMin is a fast and very easy to use PHP library that minifies given HTML5 source by r

Lars Moelleken 135 Dec 8, 2022
Convert strings between 13 naming conventions: Snake case, Camel case, Kebab case, Pascal case, Ada case, Train case, Cobol case, Macro case, Upper case, Lower case, Title case, Sentence case and Dot notation.

Case converter Use this library to convert string between: Name Method Output example ?? Camel case toCamel() myNameIsBond ??‍?? Pascal case toPascal(

Jawira Portugal 147 Dec 24, 2022
A lightweight php class for formatting sql statements. Handles automatic indentation and syntax highlighting.

SqlFormatter A lightweight php class for formatting sql statements. It can automatically indent and add line breaks in addition to syntax highlighting

Jeremy Dorn 3.9k Jan 3, 2023
Tutorial for computer vision and machine learning in PHP 7/8 by opencv (installation + examples + documentation)

Examples detect face by cascade classifier detect face by pretrained caffe model res10_300x300_ssd by ddn module detect facemarks by LBF algorithm rec

null 409 Dec 25, 2022
A PHP string manipulation library with multibyte support. Compatible with PHP 5.4+, PHP 7+, and HHVM.

A PHP string manipulation library with multibyte support. Compatible with PHP 5.4+, PHP 7+, and HHVM. s('string')->toTitleCase()->ensureRight('y') ==

Daniel St. Jules 2.5k Dec 28, 2022
YCOM Impersonate. Login as selected YCOM user 🧙‍♂️in frontend.

YCOM Impersonate Login as selected YCOM user in frontend. Features: Backend users with admin rights or YCOM[] rights, can be automatically logged in v

Friends Of REDAXO 17 Sep 12, 2022
Stop players from having spaces in their name in Pocketmin Servers.

SpaceRemover Engelish Stop players from having spaces in their name in Pocketmin Servers. Preview Help ‼ If your server does not have players with nam

Unknown Null 3 Jul 31, 2022
Highlight code blocks with league/commonmark and Shiki

Highlight code blocks with league/commonmark and Shiki This package contains a block renderer for league/commonmark to highlight code blocks using Shi

Spatie 55 Aug 27, 2022