Skip to content

Instantly share code, notes, and snippets.

@fanda
Created December 17, 2014 21:38
Show Gist options
  • Save fanda/47c2b9b71a8f6b41d188 to your computer and use it in GitHub Desktop.
Save fanda/47c2b9b71a8f6b41d188 to your computer and use it in GitHub Desktop.
Strip trailing whitespace
" Strip trailing 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
autocmd BufWritePre * :call <SID>StripTrailingWhitespaces()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment