0

The following .vimrc snippet is by Martin Tournoij's answer posted here: How do I stop the colorcolumn from breaking into multiple pieces when there are wrapped lines?

augroup wrappedcc
    autocmd!
    autocmd OptionSet colorcolumn
        \  if &cc != '' && &cc !~ ','
        \|   let &colorcolumn = join(map(range(10), {i -> &columns * i + &cc}), ',')
        \| endif

    autocmd VimResized *
        \  if len(matchlist(&cc, ',')) >= 4
        \|   let &colorcolumn = join(map(range(10), {i -> &columns * i + str2nr(&cc)}), ',')
        \| endif
augroup end

Could anybody explain why it doesn't work unless I type :set cc=78 in command mode?

That is, why it doesn't work if I simply add set cc=78 before or after it in the .vimrc itself?

An update for Vivian De Smedt

Here is my test .vimrc:

augroup wrappedcc
  autocmd!
  autocmd OptionSet colorcolumn
      \  if &cc != '' && &cc !~ ','
      \|   let &colorcolumn = join(map(range(10), {i -> &columns * i + &cc}), ',')
      \| endif

  autocmd VimResized *
      \  if len(matchlist(&cc, ',')) >= 4
      \|   let &colorcolumn = join(map(range(10), {i -> &columns * i + str2nr(&cc)}), ',')
      \| endif
augroup end

set colorcolumn=78

I also tried to put set colorcolumn=78 above the augroup instead of below it.

And here is a test file, colorcolumn-test.txt:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

When I start Vim, :set cc? returns 78 and there is only one color column, at 78:

enter image description here

:set cc?
=> 78

Then, if I type :set cc=78 in command mode, this fixes the issue:

enter image description here

:set cc?
=> 78,158,238,318,...

Vim 9.0 on Windows.

1
  • 1
    The OptionSet event isn't triggerd on Vim startup (this is documented, so the expected behaviour); I didn't know that! I updated my original answer to correct it. Aug 14 at 14:57

1 Answer 1

0

Add VimEnter autocmd to correct the colorcolumn setting after the .vimrc is read.

augroup wrappedcc
  autocmd!
  " Correct the settings after VimRC is read:
  autocmd VimEnter *
      \  if &cc != '' && &cc !~ ','
      \|   let &colorcolumn = join(map(range(10), {i -> &columns * i + &cc}), ',')
      \| endif

  " Correct the settings after colorcolumn is changed:
  autocmd OptionSet colorcolumn
      \  if &cc != '' && &cc !~ ','
      \|   let &colorcolumn = join(map(range(10), {i -> &columns * i + &cc}), ',')
      \| endif

  " Correct the setting each time the window is resized:
  autocmd VimResized *
      \  if len(matchlist(&cc, ',')) >= 4
      \|   let &colorcolumn = join(map(range(10), {i -> &columns * i + str2nr(&cc)}), ',')
      \| endif
augroup end
set colorcolumn=78
8
  • Well, but this doesn't answer my question. The only problem I have with this snippet is that it works if I start Vim and then type :set cc=78 in command mode. But it doesn't work if I add set cc=78 before or after it in .vimrc itself.
    – john c. j.
    Aug 14 at 7:16
  • It should :-) Could you share your .vimrc ? Could you try to set cc at the end of it? What is the result of the command set cc? It seems to mz that some part of your .vimrc unset cc. Aug 14 at 7:20
  • See the updated question :-)
    – john c. j.
    Aug 14 at 7:42
  • Thanks for the update. Is your set cc positioned after the autocmd? Aug 14 at 8:04
  • 1
    It seems it was simply required to replace OptionSet colorcolumn with autocmd BufReadPost,BufNewFile *. Duh...
    – john c. j.
    Aug 14 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.