wnd's weblog

Vim and long lines

4 Dec 2013 18:29:11 software
highlight BadFormatting cterm=NONE ctermbg=red ctermfg=black guibg=red guifg=black
match BadFormatting /\s\+$\| \+\ze\t\|\%<85v\%>80v./

Some months ago I switched to i3 window manager after discovering a Vim option to highlight column 81. As silly as it may sound, not easily spotting long lines was one of the biggest reason for not switching earlier. However, colorcolumn option wasn’t perfect. It messed up using X11 to paste text from Vim.

When I first grew tired to colorcolumn and loads and loads of unwanted whitespaces in pasted text, I figured I’d use X resources to configure rxvt to draw column markers. These markers would obviously be completely separate from actual text buffer. After struggling to introduce X resources in rxvt, I realised my plan also was flawed; this wouldn’t work properly with Vim’s windows.

When the time to have a Christmas party at work arrived, I realised I’d have quality time to work on this again. I patched rxvt with a private set of VT escape sequences to manage a list of column markers. Adding this functionality was surprisingly simple and the unminimised patch (without partially implemented X resources) was just 145 lines. The next step would be to create a Vim script, or in worst case scenario, patch Vim to send required sequences to set the markers.

When I was testing my fancy little patch, I came across some settings in Vim configuration file I had added for highlighting redundant whitespaces a decade earlier. That’s when I realised I could use exactly same thing for highlighting long lines. It wouldn’t be exactly as pretty as my fancy little patch, but there would be no patching and no compiling.

The next time I think I need to patch some piece of software, I really need to sit on my hands and think if it’s really the only way. That would have saved a lot of time.

Update: 21 Sep 2019 - updated matcher to modern version