Trailing Whitespaces in VIM

Trailing whitespaces don’t bother me in general, let’s be clear, I don’t like them but there are other thousands things that bother me more. Although I hate (with hate, I mean it) when someone sends me a patch and I can’t apply it cleanly because of the trailing whitespaces… in this moment I understand this guy and this other trailing whitespaces hater.

Since I use Vim I leave here some solutions to remove and highlight them automatically.

Add to your .vimrc


autocmd FileType c,cpp,vhd,h,sv,svh autocmd BufWritePre <buffer> %s/\s\+$//e



highlight ExtraWhitespace ctermbg=red guibg=red
match ExtraWhitespace /\s\+$/
autocmd BufWinEnter * match ExtraWhitespace /\s\+$/
autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
autocmd InsertLeave * match ExtraWhitespace /\s\+$/
autocmd BufWinLeave * call clearmatches()


Got it from: