sponsor Vim development Vim logo Vim Book Ad

DeleteTrailingWhitespace : Delete unwanted whitespace at the end of lines.

 script karma  Rating 26/10, Downloaded by 656    Comments, bugs, improvements  Vim wiki

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

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.

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).

USAGE
:[range]DeleteTrailingWhitespace
                        Delete all trailing whitespace in the current buffer
                        or [range].
 
install details
INSTALLATION
This script is packaged as a vimball. If you have the "gunzip" decompressor
in your PATH, simply edit the *.vba.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.vba.gz
    :so %
To uninstall, use the :RmVimball command.

DEPENDENCIES
- 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.

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

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.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: 54.167.41.199

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.
   
SourceForge.net Logo