DeleteTrailingWhitespace : Delete unwanted whitespace at the end of lines.
| script karma
Downloaded by 1140
Comments, bugs, improvements
script versions (upload new version)
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
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.
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
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.
Click on the package to download.
ip used for rating: 18.104.22.168