sponsor Vim development Vim logo Vim Book Ad

DeleteTrailingWhitespace : Delete unwanted whitespace at the end of lines.

 script karma  Rating 101/35, Downloaded by 3611  Comments, bugs, improvements  Vim wiki

created by
Ingo Karkat
script type
This plugin deletes whitespace at the end of each line, on demand via the
:DeleteTrailingWhitespace command, or automatically when the buffer is

This plugin leverages the superior detection and customization facilities of
the companion ShowTrailingWhitespace.vim plugin (vimscript #3966), though it
can also be used without it if you're not interested in highlighting and

To quickly locate the occurrences of trailing whitespace, you can use the
companion JumpToTrailingWhitespace.vim plugin (vimscript #3968).

The basic substitution commands as well as more elaborate scriptlets, as the
idea of automating this can be found in this VimTip:
There are already a number of plugins that define such a command; most bundle
this functionality together with the highlighting of trailing whitespace.
However, most of them cannot consider whitespace exceptions and are not as
- trailing-whitespace (vimscript #3201) defines :FixWhitespace.
- bad-whitespace (vimscript #3735) defines :EraseBadWhitespace.
- Trailer Trash (vimscript #3938) defines :Trim.
- StripWhiteSpaces (vimscript #4016) defines :StripWhiteSpaces and strips
  automatically, too, but is way more simple than this plugin.
- WhiteWash (vimscript #3920) provides functions that also can strip visible
  ^M and sequential whitespacw between words.

                        Delete all trailing whitespace in the current buffer
                        or [range].
install details
This script is packaged as a vimball. If you have the "gunzip" decompressor
in your PATH, simply edit the *.vmb.gz package in Vim; otherwise, decompress
the archive first, e.g. using WinZip. Inside Vim, install by sourcing the
vimball or via the :UseVimball command.
    vim DeleteTrailingWhitespace*.vmb.gz
    :so %
To uninstall, use the :RmVimball command.

- Requires Vim 7.0 or higher.
- The ShowTrailingWhitespace.vim plugin (vimscript #3966) complements this
  script, but is not required. With it, this plugin considers the whitespace
  exceptions for certain filetypes.

For a permanent configuration, put the following commands into your vimrc:

This plugin doesn't come with predefined mappings, but if you want some, you
can trivially define them yourself:
    nnoremap <Leader>d$ :<C-u>%DeleteTrailingWhitespace<CR>
    vnoremap <Leader>d$ :DeleteTrailingWhitespace<CR>

By default, trailing whitespace is processed before writing the buffer when it
has been detected and is currently being highlighted by the
ShowTrailingWhitespace.vim plugin.
To turn off the automatic deletion of trailing whitespace, use:
    let g:DeleteTrailingWhitespace = 0
If you want to eradicate all trailing whitespace all the time, use:
    let g:DeleteTrailingWhitespace = 1

For processing, the default action is aborting the write, unless ! is given.
To automatically eradicate the trailing whitespace, use:
    let g:DeleteTrailingWhitespace_Action = 'delete'
To ask whether to remove or keep the whitespace (either for the current
buffer, or all buffers in the entire Vim session), use:
    let g:DeleteTrailingWhitespace_Action = 'ask'

When the plugin is configured to ask the user, and she answers the query with
"Never" or "Nowhere", the ShowTrailingWhitespace.vim highlighting is turned
off automatically; when you ignore it, there's typically no sense in still
highlighting it. If you don't want that, turn it off via:
    let g:DeleteTrailingWhitespace_ChoiceAffectsHighlighting = 0

The global detection and processing behavior can be changed for individual
buffers by setting the corresponding buffer-local variables.

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
DeleteTrailingWhitespace-1.06.vmb.gz 1.06 2015-02-24 7.0 Ingo Karkat - FIX: Warning for nomodifiable buffer does not consider buffer-local b:DeleteTrailingWhitespace_Action (after version 1.05).
DeleteTrailingWhitespace-1.05.vmb.gz 1.05 2014-12-12 7.0 Ingo Karkat - Corner case: Avoid "E21: Cannot make changes, 'modifiable' is off" on a nomodifiable buffer when g:DeleteTrailingWhitespace_Action = 'delete', and instead just show a warning. Thanks to Enno Nagel for raising this issue.
DeleteTrailingWhitespace-1.04.vmb.gz 1.04 2013-12-14 7.0 Ingo Karkat - Minor: Make substitute() robust against 'ignorecase'.
- Minor: Correct lnum for no-modifiable buffer check.
- Handle local exception regular expressions that contain a "/" character. This must be escaped for the :substitute/ command.
DeleteTrailingWhitespace.vba.gz 1.03 2012-04-19 7.0 Ingo Karkat Handle readonly and nomodifiable buffers by printing just the warning / error, without the multi-line function error.
DeleteTrailingWhitespace.vba.gz 1.02 2012-04-14 7.0 Ingo Karkat FIX: Avoid polluting search history.FIX: Avoid polluting search history.
DeleteTrailingWhitespace.vba.gz 1.01 2012-04-04 7.0 Ingo Karkat Define command with -bar so that it can be chained.
DeleteTrailingWhitespace.vba.gz 1.00 2012-03-16 7.0 Ingo Karkat Initial upload
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
OSDN.net Logo