Skip to content

Instantly share code, notes, and snippets.

@snusnu
Created June 6, 2013 14:49
Show Gist options
  • Save snusnu/5722076 to your computer and use it in GitHub Desktop.
Save snusnu/5722076 to your computer and use it in GitHub Desktop.
vimrc snippet to automatically strip trailing whitespace in code files
" a function that preserves the state
" see http://technotales.wordpress.com/2010/03/31/preserve-a-vim-function-that-keeps-your-state/
function! Preserve(command)
" Preparation: save last search, and cursor position.
let _s=@/
let l = line(".")
let c = col(".")
" Do the business:
execute a:command
" Clean up: restore previous search history, and cursor position
let @/=_s
call cursor(l, c)
endfunction
autocmd BufWritePre *.rb,*.js,*.coffee,*.rdoc,*.rake,Rakefile,Gemfile,Thorfile,Guardfile,config.ru,*.html,*.css,*.md,*.markdown,*.textile,*.html.erb,LICENSE,*.treetop :call Preserve("%s/\\s\\+$//e")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment