Created
June 6, 2013 14:49
-
-
Save snusnu/5722076 to your computer and use it in GitHub Desktop.
vimrc snippet to automatically strip trailing whitespace in code files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" 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