sponsor Vim development Vim logo Vim Book Ad

DeleteTrailingWhitespace : Delete unwanted whitespace at the end of lines.

 script karma  Rating 114/41, Downloaded by 4938  Comments, bugs, improvements  Vim wiki

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

SEE ALSO
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
customizing.

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

RELATED WORKS
The basic substitution commands as well as more elaborate scriptlets, as the
idea of automating this can be found in this VimTip:
    http://vim.wikia.com/wiki/Remove_unwanted_spaces
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
flexible.
- 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.
- strip_trailing_whitespace.vim (vimscript #5695) is another alternative which
  offers a command and configurable mappings.

USAGE
:[range]DeleteTrailingWhitespace
                        Delete all trailing whitespace in the current buffer
                        or [range].
 
install details
INSTALLATION
The code is hosted in a Git repo at
    https://github.com/inkarkat/vim-DeleteTrailingWhitespace
You can use your favorite plugin manager, or "git clone" into a directory used
for Vim packages. Releases are on the "stable" branch, the latest unstable
development snapshot on "master".

This script is also 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.

DEPENDENCIES
- Requires Vim 7.0 or higher.
- Requires the ingo-library.vim plugin (vimscript #4433), version 1.036 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.

CONFIGURATION
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
Also see the following configuration option to turn off the confirmation
queries in case you want unconditional, fully automatic removal.

For processing, the default asks whether to remove or keep the whitespace
(either for the current buffer, or all buffers in the entire Vim session, or
for the current buffer across time and Vim sessions).
Alternatively, the plugin can just abort the write, unless ! is given:
    let g:DeleteTrailingWhitespace_Action = 'abort'
To automatically eradicate the trailing whitespace, use:
    let g:DeleteTrailingWhitespace_Action = 'delete'

When the plugin is configured to ask the user, and she gives a negative
answer, 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, keep the highlighting via:
    let g:DeleteTrailingWhitespace_ChoiceAffectsHighlighting = []
or selectively keep the highlighting by removing responses from:
    let g:DeleteTrailingWhitespace_ChoiceAffectsHighlighting =
    \ ['No', 'Never', 'Never ever', 'Never ever recalled', 'Never ever highlight', 'Nowhere']

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.10.vmb.gz 1.10 2020-02-03 7.0 Ingo Karkat - ENH: Add "Forever" and "Never ever" choices that persist the response for the current file across Vim session.
- ENH: If ShowTrailingWhitespace.vim (version 1.10 or higher) is installed, offer a "Never ever highlight" choice that turns off highlighting for the current file persistently across Vim sessions.
- CHG: Make asking for action (instead of aborting) the default action, now that we offer many more choices, and the whole persistent recall of responses only works there. It's also more friendly and transparent towards first-time users of the plugin. To restore the original behavior: > let g:DeleteTrailingWhitespace_Action = 'abort'
- CHG: g:DeleteTrailingWhitespace_ChoiceAffectsHighlighting now is a List of possible responses that if missing will keep the highlighting for that particular response.
- BUG: DeleteTrailingWhitespace_Action checks window-local instead of buffer-local override. Thanks to Enno for noticing.
- Add dependency to ingo-library (vimscript #4433). *** You need to separately install ingo-library (vimscript #4433) version 1.036 (or higher)! ***
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: 18.223.171.12

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.
   
Vim at Github