Tag Archives: vim

Getting rid of whitespace

No, not storage related but more towards coding scripts etc and assuring your git repositories do not show up with huge diff sections you need to correct. Just a little tip and a “note to self”.

If you’ve event been keen enough to not use an IDE for whatever language you use and kept to a real editor (VIM obviously.. :-)) you may have encountered the phenomenon that whitespace at the end of lines is a nasty thing to look at when you start putting stuff into version control repositories like Subversion or GIT. A little change from some copy or past action may leave you with a “git diff” of a couple of hundred lines you need to correct.

To fix that simply let VIM clear out all empty whitespace (tabs, spaces, etc.) by having these removed before the actual write to disk.

To do that simply add

autocmd BufWritePre *.sh :%s/\s\+$//e

to your ~/.vimrc and with every :w the substitute function driven by the regex after the colon will remove it all in all shell scripts (*.sh). Obviously you can add every extension you need here.  Very handy.