Vim: make text go red or screen flash after 80 column mark?

1

Is there a way to make Vim warn me after a line has exceeded n characters?

Something simple like a screen flash or the text go bold red or something.

!! Bonus points if n can be made a variable that is easy to set in each individual window (I use 80 columns usually, but 100 columns for PHP).

iDontKnowBetter

Posted 2012-08-27T19:16:17.480

Reputation: 273

Answers

5

There are two possible solutions:

  1. If your version of Vim is at least 7.3, just set 'colorcolumn' option (see :help 'colorcolumn'), e.g. to 81:

    set colorcolumn=81
    

    It can also highlight more than one column:

    let &colorcolumn = join(range(81, 400), ',')
    
  2. If you have Vim version prior to 7.3, you may want to define custom highlighting rule to highlight all characters after specific column, e.g.:

    syn match tooLong /\%81c.*$/
    hi link tooLong Error
    

To have different settings for different file types, put this commands into appropriate filetype plugin (for example to ~/.vim/ftplugin/cpp.vim). Or define autocommands (see :help :autocmd) triggered by FileType event (see :help FileType) in your .vimrc.

xaizek

Posted 2012-08-27T19:16:17.480

Reputation: 891

1colorcolumn doesn't add only one column: set colorcolumn=81,82,83 or let &colorcolumn=join(range(81,999),",") – Chai T. Rex – 2018-08-30T02:36:25.363

1@ChaiT.Rex, thank you for the correction, I somehow missed this back then. Updated the answer to fix this, weird wording, formatting and provide hyperlinks to the documentation. By the way, 999 is too much, Vim won't highlight more than 256 columns. – xaizek – 2018-08-31T09:34:17.180

thanks, colorcolumn serves the purpose. – iDontKnowBetter – 2012-08-28T00:34:57.323

or if you want to shorten it :set cc=80 – Braden – 2012-09-02T16:01:27.310