I am getting 'trailing whitespace' errors trying to commit some files in git.
I want to remove these trailing whitespace characters automatically right before I save python files.
Can you configure vim to do this? If so, how?
I am getting 'trailing whitespace' errors trying to commit some files in git.
I want to remove these trailing whitespace characters automatically right before I save python files.
Can you configure vim to do this? If so, how?
I found the answer here (http://vim.wikia.com/wiki/Remove_unwanted_spaces#Automatically_removing_all_trailing_whitespace).
Adding the following to my .vimrc file did the trick.
autocmd BufWritePre *.py :%s/\s\+$//e
I also usually have a :
match Todo /\s\+$/
in my .vimrc
file, so that end of line whitespace are hilighted.
Todo being a syntax hilighting group-name that is used for hilighting keywords like TODO
, FIXME
or XXX
. It has an annoyingly ugly yellowish background color, and I find it's the best to hilight things you don't want in your code :-)
This is how I'm doing it. I can't remember where I stole it from tbh.
autocmd BufWritePre * :call <SID>StripWhite()
fun! <SID>StripWhite()
%s/[ \t]\+$//ge
%s!^\( \+\)\t!\=StrRepeat("\t", 1 + strlen(submatch(1)) / 8)!ge
endfun
Compilation of above plus saving cursor position:
fun! <SID>StripTrailingWhitespaces()
let l = line(".")
let c = col(".")
%s/\s\+$//e
call cursor(l, c)
endfun
autocmd BufWritePre *.h :call <SID>StripTrailingWhitespaces()