Whitespace at end of line

On Coding Horror Jeff wrote about whitespace at the end of lines of code. I hate that as much as the next guy. It seems irrational, but I think it's not. It has no function and only creates problems on times when you don't expect them, especially with source code control tools.

So I wrote a small piece of Vim script code to highlight the spaces at the end of the line in bright red.

highlight OverLength ctermbg=darkred ctermfg=white guibg=#FFD9D9

function! EndOfLineWhitespace()
    3match OverLength /\s\+$/
endfunction

call EndOfLineWhitespace()

This will highlight all the whitespace at the end of a line. If you don't like to use this for all files, you can use the usual ways to do this in Vim.

UPDATE: Since Vim 7.2 it is possible to use another function instead of the match functions. It's called matchadd. The solution I gave above can be written as follows:

highlight OverLength ctermbg=darkred ctermfg=white guibg=#FFD9D9
call matchadd('OverLength', '\s\+$', -1)

See the help documentation for matchadd to see how this works.

Via VimTip810

Welcome

My name is Peter Stuifzand. You're reading my personal website.

Profiles