Search code examples
vimword-wrap

Colorcolumn and text wrapping at the same time in Vim


I am setting colorcolumn in Vim, on the 90th column, but at the same time I would like to maintain text wrapping functionality.

Therefore, in order to maintain the coloured column on the wrapped lines, and have no breaks, I have to put in my .vimrc something like:

let col_no=90
if ((longest_line_size / 90) >= 2)
  for i in range(2, (longest_line_size / 90))
    let col_no=col_no . "," . ((90*i) + (&columns - 91))
  endfor
endif
execute 'set colorcolumn=' . col_no

The problem is that when I resize the window (or the buffer), the coloured column on the wrapped lines is not aligned. A solution would be to re-run the function when the buffer is resized.

Is this possible? Any other solutions/suggestions are also welcome.


Solution

  • You can run a function every time the window size changes using the following autocommand

    augroup Resize
      autocmd!
      autocmd VimResized * :call YourFunctionName<cr>
    augroup END
    

    Add that to your vimrc file. To use it with your code you will need to wrap the code up in a function.

    function YourFunctionName
      #your code goes here
    endfunction
    

    Note that the function name must begin with a capital letter.