vim: automatically highlight long lines

This .vimrc snippet highlights lines when you exceed 77 columns - this is especially useful if you are trying to adhere to PEP8 with Python development.

The if statement makes this work for older vims as well as more recent versions which is handy if you put your .vimrc on remote boxes as vim below v7.1.40 doesn't have the matchadd function.

" Highlight lines over 77 columns
if has('matchadd')
    :au BufWinEnter * let w:m1=matchadd('Search', '\%<81v.\%>77v', -1)
    :au BufWinEnter * let w:m2=matchadd('ErrorMsg', '\%>80v.\+', -1)
else
    :au BufRead,BufNewFile * syntax match Search /\%<81v.\%>77v/
    :au BufRead,BufNewFile * syntax match ErrorMsg /\%>80v.\+/
endif

To configure highlighting to work manually see the vim wiki for more details http://vim.wikia.com/wiki/Highlight_long_lines

Whilst I've often heard people snub the PEP8 recommendation for keeping line length to under 79 chars, I've personally found it leads to more readable code whether you live in a terminal or use gui editors. YMMV

Show Comments