The colorcolumn
setting accepts multiple values, for example set colorcolumn=30,60
will highlight column 30 and 60.
We can use this to display a single unbroken line for wrapped lines; for example for a 130-column line on a 40 column wide display you would use set colorcolumn=30,70,110
to highlight the first three columns.
You can set this automatically with a little command:
command! -nargs=1 WrappedCC let &colorcolumn = join(map(range(10), {i -> &columns * i + <args>}), ',')
This uses &columns
to get the screen width, and adds the first ten colour columns at the correct place.
You can also hook in to OptionSet
autocommand to automatically do this whenever colorcolumn
is set, so you don't need a new command; you can just use set cc=80
instead of WrappedCC 80
. You can still use set cc=
to clear the highlight, or set cc=80,120
to only set those two columns.
fun s:wrapped_cc()
let &colorcolumn = join(map(range(10), {i -> &columns * i + str2nr(&cc)}), ',')
endfun
augroup wrappedcc
autocmd!
autocmd OptionSet colorcolumn if &cc != '' && &cc !~ ',' | call s:wrapped_cc() | endif
autocmd VimEnter * if &cc != '' && &cc !~ ',' | call s:wrapped_cc() | endif
autocmd VimResized * if len(matchlist(&cc, ',')) >= 4 | call s:wrapped_cc() | endif
augroup end
This also needs to hook in to the VimEnter
because OptionSet
isn't triggered on startup; otherwise set colorcolumn=80
from your vimrc won't work; since the VimEnter
is triggered after reading all the vimrc you can put the set colorcolumn=
anywhere in your vimrc (it doesn't need to be after the autocmd).
Also need to hook in to the VimResized
to re-calculate the column positions because they need to be adjusted after the window is resized. This assumes that anything with more than four columns is a "wrapped column".