wyattanderson.com

Hey there. I'm Wyatt Anderson. Not the construction company owner, or biology professor, or the singer, but the software engineer. I'll post some stuff on here from time to time, but you're more likely to hear from me on Twitter or something. Feel free to get in touch.

Highlighting long lines in vim

I’ve become a bit obsessive about wrapping my lines to 80 columns, whether code or text. It’s an aesthetic thing for me, mostly, but there are practical benefits. I autowrap comments in vim with formatoptions+=c and set textwidth=79, but I don’t want to autowrap code because I sometimes I think it’s appropriate to extend a few characters over, or the file might be a legacy file that where a formatting diff is unwelcome. So, for those times, I use the following lines in my vimrc to progressively highlight long lines: lines 80-90 characters in width are highlighted with a subtle shade of red, while lines longer than 90 characters are highlighted in a more noticable shade of red:

" Highlight lines longer than 80 characters as dark-red, lines longer than 90
" characters as a brighter red.
augroup vimrc_autocmds
    autocmd BufEnter * highlight OverLength ctermbg=124 guibg=#990000
    autocmd BufEnter * highlight SortaOverLength ctermbg=52 guibg=#330000
    autocmd BufEnter * match SortaOverLength /\m\%>80v.\%<91v/
    autocmd BufEnter * 2match OverLength /\m\%>90v.\%<140v/
augroup END

Two matches can be active at any one time in a vim document, so we match each portion of the line separately. Colors can be configured to your liking, obviously, and are currently set for terminals with a dark background and 256-color command-line vim. Additionally, it won’t highlight past 140 characters to prevent slowdown with really, really long lines.