Skip to content

Instantly share code, notes, and snippets.

@sumeet
Created June 10, 2015 22:29
Show Gist options
  • Save sumeet/5032e9dc26efa44fad85 to your computer and use it in GitHub Desktop.
Save sumeet/5032e9dc26efa44fad85 to your computer and use it in GitHub Desktop.
" Remove extra whitespace before saving
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