Vim Tips Wiki
(Fix typos)
(Adjust previous/next navigation)
Line 3: Line 3:
 
|id=810
 
|id=810
 
|previous=809
 
|previous=809
|next=811
+
|next=812
 
|created=October 25, 2004
 
|created=October 25, 2004
 
|complexity=intermediate
 
|complexity=intermediate

Revision as of 03:10, 30 June 2008

Tip 810 Printable Monobook Previous Next

created October 25, 2004 · complexity intermediate · author Nitin Raut · version 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 beyond 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.

Comments

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.


Those who find that every line is highlighted probably have textwidth set to 0. 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.


This will force Vim to execute the autocommand manually:

:set tw=80 | do BufEnter

See :help :autocmd-execute.


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 character on all the lines which have 80 characters or more. You can map this one for a fast highlighting.


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.


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 need the 80 column highlight frequently, you may want to make it a permanent feature of your vim. Simply put this in the appropriate vimrc file (either /etc/vim/vimrc or ~/.vimrc):

" Highlight definitions
" Near80Col will make an ordinary terminal bold/italic, a colour terminal
" (or gui) will have italic blue text on a yellow background
:highlight Near80ColLimit term=italic,bold cterm=italic ctermbg=yellow ctermfg=darkblue gui=bold,italic guibg=yellow guifg=darkblue
" Over80Col will inverse text on a standard terminal, and white-on-red on a
" colour terminal
:highlight Over80ColLimit term=inverse,bold cterm=bold ctermbg=red ctermfg=white gui=bold guibg=red guifg=white

" Matches must be defined as a syntax-match, because a normal match
" is cleared by any searches. Also, only one match rule can exist
" at any given time

" Match any text in cols 71 .. 80 as "Near the 80 column limit"
:syntax match Near80ColLimit /\%<81v.\%>71v/
" Match everything over 81 cols as "Over 80 Column Limit" highlight
:syntax match Over80ColLimit /\%81v.*/
"only one match rule can exist at any given time" – this is only partially correct. Only one match using :match can exist at one time, but there are also :2match, :3match, and the matchadd() function that can be used to define more match rules.