Skip to content

Instantly share code, notes, and snippets.

@johntyree
Created August 9, 2012 09:26
Show Gist options
  • Save johntyree/3302642 to your computer and use it in GitHub Desktop.
Save johntyree/3302642 to your computer and use it in GitHub Desktop.
Remove trailing whitespace in Vim.
" Remove trailing whitespace
command! StripTrailingWhitespaces call <SID>StripTrailingWhitespaces()
nmap ,ws :StripTrailingWhitespaces<CR>
" Strip whitespace
function! <SID>StripTrailingWhitespaces()
" Preparation: save last search, and cursor position.
let _s=@/
let l = line(".")
let c = col(".")
" Do the business:
%s/\s\+$//e
" Clean up: restore previous search history, and cursor position
let @/=_s
call cursor(l, c)
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment