From 021b4aa2e232637268d236fdf2e861bf3d92ad6e Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Fri, 16 Sep 2011 11:27:20 -0700 Subject: [PATCH] Do trailing whitespace clean up on write - Move whitespace clean up to a function triggered by an autocmd --- vimrc | 53 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/vimrc b/vimrc index 024d228..f6e2219 100644 --- a/vimrc +++ b/vimrc @@ -99,23 +99,6 @@ endif set modeline set modelines=12 -if has('autocmd') - filetype plugin indent on - " spelling for text files - autocmd FileType text set spell - " spaces as tabs for python - autocmd filetype python setlocal expandtab - " don't show tabs in html and xml - autocmd filetype html,xml set listchars-=tab:▸\ - - " Jump to last known cursor position unless it's the first line, or past the - " end of the file - autocmd BufReadPost * - \ if line("'\"") > 1 && line("'\"") <= line("$") | - \ exe "normal! g`\"" | - \ endif -endif - " use a colorscheme if the terminal can support it (or we're in a GUI) if &t_Co >= 256 || has('gui_running') if has('gui_running') @@ -172,9 +155,22 @@ nnoremap k nnoremap l +function! StripTrailingWhitespace() + " save last search + let _s=@/ + " save cursor position + let l = line('.') + let c = col('.') + " do the clean up + %s/\s\+$//e + " restore saved stuff + let @/=_s + call cursor(l, c) +endfunction + let mapleader=',' " strip all trailing whitespace in the current file -nnoremap W mkHml:%s/\v\s+$//`lzt`k +nnoremap W :call StripTrailingWhitespace() " edit and source my .vimrc nmap ev :e $MYVIMRC nmap sv :so $MYVIMRC @@ -182,3 +178,24 @@ nmap sv :so $MYVIMRC nmap :nohlsearch " find all nmap fa :%s/\v + + +if has('autocmd') + filetype plugin indent on + " spelling for text files + autocmd FileType text set spell + " spaces as tabs for python + autocmd filetype python setlocal expandtab + " don't show tabs in html and xml + autocmd filetype html,xml set listchars-=tab:▸\ + + " Jump to last known cursor position unless it's the first line, or past the + " end of the file + autocmd BufReadPost * + \ if line("'\"") > 1 && line("'\"") <= line("$") | + \ exe "normal! g`\"" | + \ endif + + " Clean whitespace before saving + autocmd BufWritePre *.py,*.c,*.html :call StripTrailingWhitespace() +endif