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).


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.

  • thanks, colorcolumn serves the purpose. – iDontKnowBetter Aug 28 '12 at 0:34
  • or if you want to shorten it :set cc=80 – Braden Sep 2 '12 at 16:01
  • 1
    colorcolumn doesn't add only one column: set colorcolumn=81,82,83 or let &colorcolumn=join(range(81,999),",") – Chai T. Rex Aug 30 '18 at 2:36
  • 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 Aug 31 '18 at 9:34

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.