sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #810: Highlighting Text Beyond 80 Columns

 tip karma   Rating 73/35, Viewed by 3072 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   October 25, 2004 0:13      complexity:   intermediate
author:   Nitin Raut      as of Vim:   5.7


Due to oversightedness we are often left with lines of text longer than 80 columns or the set textwidth. One way to spot such lines is to highlight the text  beyond textwidth.

I found that lines beyong textwidth in .c and .h files can be highlighted using the below line in .vimrc.

au BufNewFile,BufRead *.c,*.h exec 'match Todo /\%>' .  &textwidth; . 'v.\+/'

you may add different file extensions for which you may wish to highlight text after textwidth.

Regards,
Nitin Raut

 rate this tip  Life Changing Helpful Unfulfilling 

<< GNU\Linux - gvim as an external editor | Highlight whitespace errors in files >>

Additional Notes

Anonymous, October 25, 2004 23:31
just set the linebreak character/string to mark wrapped lines, e.g.
:set showbreak=NEWLINE:  
or
:set showbreak=>

You may need to set other flags like linebreak, textwidth, etc..

Thomas
[email protected], October 27, 2004 14:12
Nitin, is 'textwidth' a variable you have to set? Your command highlights every line in the file.
Anonymous, October 27, 2004 17:38
I have tried but it highlights everything. Do I need a certain setup for this ?
Anonymous, October 27, 2004 22:12
Those who find that every line is highlighted probably have textwidth set to 0

For details see:-
:help textwidth

My own mod was to add a conditional check and only highlight if textwidth was > 0

    au BufEnter *   if &textwidth; > 0 | exec 'match Todo /\%>' . &textwidth; . 'v.\+/' | endif

Note this runs on BufEnter for all files.
[email protected], October 28, 2004 5:55
Is it possible to draw a red line at the 80th column? I don't like the text marked but it would be nice to see that the line is too long.


Sven
Anonymous, October 28, 2004 7:51
Textwidth must be set before your setting, right ? When I set, e.g. tw=80 manually after entering the buffer, highlighting does not work, unfortunately. If I set it before, it works as expected. Any hint how to make a kind of refresh ?
Anonymous, October 28, 2004 9:11
This will force Vim to execute the autocommand manually:-

  :set tw=80 | do BufEnter

See :help *autocmd-execute*

-Frank.
Eric Boucher, November 2, 2004 15:47
If you want to highlight only the 80th column with a search for example, you can do:
/.\{79}\zs.

This will 'draw' a vertical line on the 80th caracter on all the lines which have 80 caracters or more. You can map this one for a fast highlighting.
Anonymous, November 28, 2004 9:26
Don't get over complex, there is a search designed to do exactly this:

Highlight all characters at column #81 (all longer than 80 characters)

\%81c

Highlight all characters from column #81 to the end of the line.

\%>81c

I have those searches mapped to keys, but you could just as easily add them to a syntax file or a highlight/match pattern.
Anonymous, January 28, 2007 17:00
note that the 'c' after \%80 means that the 80th character gets matched and that tabs are characters also... so if you for example write a program and use tabs to indent your code, this is probably not what you want, especially if you are going to print the code later.
use 'v' instead in this case.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
   
Sponsored by Web Concept Group Inc. SourceForge.net Logo