Skip to content

Instantly share code, notes, and snippets.

@ajorgensen
Created July 15, 2013 13:15
Show Gist options
  • Save ajorgensen/5999848 to your computer and use it in GitHub Desktop.
Save ajorgensen/5999848 to your computer and use it in GitHub Desktop.
Remove trailing whitespace automatically in vim
if has("autocmd")
autocmd BufWritePre {*.rb,*.js,*.coffee,*.scss,*.haml,*.py,*.js} :call <SID>StripTrailingWhitespaces()
endif
nnoremap <silent> <F5> :call <SID>StripTrailingWhitespaces()<CR>
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