Skip to content

Instantly share code, notes, and snippets.

@jeffrafter
Created September 14, 2012 21:03
Show Gist options
  • Save jeffrafter/3724808 to your computer and use it in GitHub Desktop.
Save jeffrafter/3724808 to your computer and use it in GitHub Desktop.
" Functions
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
" autocommands
if has('autocmd')
autocmd BufWritePre * :call <SID>StripTrailingWhitespaces()
endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment