created 2005 · complexity intermediate · author Bertram Scharpf · version 6.0
In a search,
\s finds whitespace (a space or a tab), and
\+ finds one or more occurrences.
The following command deletes any trailing whitespace at the end of each line. If no trailing whitespace is found no change occurs, and the
e flag means no error is displayed.
The following deletes any leading whitespace at the beginning of each line.
:%s/^\s\+//e " Same thing (:le = :left = left-align given range; % = all lines): :%le
With the following mapping a user can press F5 to delete all trailing whitespace. The variable
_s is used to save and restore the last search pattern register (so next time the user presses
n they will continue their last search), and
:nohl is used to switch off search highlighting (so trailing spaces will not be highlighted while the user types). The
e flag is used in the substitute command so no error is shown if trailing whitespace is not found. Unlike before, the substitution text must be specified in order to use the required flag.
:nnoremap <silent> <F5> :let _s=@/ <Bar> :%s/\s\+$//e <Bar> :let @/=_s <Bar> :nohl <Bar> :unlet _s <CR>
See Highlighting whitespaces at end of line to display, rather than delete, unwanted whitespace.
The following is a more elaborate procedure that can display or remove unwanted whitespace. Here, "unwanted" means any spaces before a tab character, or any space or tab at the end of a line.
function ShowSpaces(...) let @/='\v(\s+$)|( +\ze\t)' let oldhlsearch=&hlsearch if !a:0 let &hlsearch=!&hlsearch else let &hlsearch=a:1 end return oldhlsearch endfunction function TrimSpaces() range let oldhlsearch=ShowSpaces(1) execute a:firstline.",".a:lastline."substitute ///gec" let &hlsearch=oldhlsearch endfunction command -bar -nargs=? ShowSpaces call ShowSpaces(<args>) command -bar -nargs=0 -range=% TrimSpaces <line1>,<line2>call TrimSpaces() nnoremap <F12> :ShowSpaces 1<CR> nnoremap <S-F12> m`:TrimSpaces<CR>`` vnoremap <S-F12> :TrimSpaces<CR>
This is a similar function which similates the manual steps for removing the whitespace.
function StripTrailingWhitespace() if !&binary && &filetype != 'diff' normal mz normal Hmy %s/\s\+$//e normal 'yz<CR> normal `z endif endfunction
However, this has minor side-effects, such as influencing undo history and sometimes changing scroll position.
One way to make sure to remove all trailing whitespace in a file is to set an
autocmd in your .vimrc file. Every time the user issues a
:w command, Vim will automatically remove all trailing whitespace before saving.
autocmd BufWritePre * %s/\s\+$//e
However, this is a potentially dangerous autocmd to have as it will always strip trailing whitespace from every file a user saves. Sometimes, trailing whitespace is desired, or even essential in a file so be careful when implementing this
One method to mitigate this issue in a .vimrc file, where trailing whitespace matters, is to change how .vimrc prepends wrapped lines. For example, add the following into the .vimrc:
set wrap set linebreak " note trailing space at end of next line set showbreak=>\ \ \
Now when saving the .vimrc it will use
"> \" instead of
"> " to prepend wrapped lines.
A user can also specify a particular filetype in an
autocmd so that only that filetype will be changed when saving. The following only changes files with the extension .pl:
autocmd BufWritePre *.pl %s/\s\+$//e
Additionally, a FileType autocommand can be used to restrict the
autocmd to certain file types only.
autocmd FileType c,cpp,java,php autocmd BufWritePre <buffer> %s/\s\+$//e
- :help :autocmd - Additional information on
- The plugin offers a eponymous command, and can remove trailing whitespace automatically when the buffer is written.
- The plugin defines :FixWhitespace.
- The plugin defines :EraseBadWhitespace.
- The plugin defines :Trim.
- The vim-strip-trailing-whitespace plugin touches only modified lines on save.