sponsor Vim development Vim logo Vim Book Ad

basic Tip #811: Highlight whitespace errors in files

 tip karma   Rating 98/34, Viewed by 2139 

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

created:   October 28, 2004 9:42      complexity:   basic
author:   Frank B      as of Vim:   5.7

Being somewhat of a neat-freak, I dislike redundant trailing spaces,
and redundant space chars embedded inside of leading spaces.

This auto-command will highlight both of these when added to your vimrc.

    au Syntax *    syn match Error /\s\+$/ | syn match Error /^\s* \t\s*/

You'll be surprised how much whitespace crud there is in source files everywhere.

-Frank.

 rate this tip  Life Changing Helpful Unfulfilling 

<< Highlighting Text Beyond 80 Columns | Keep modified [+] flag after writing to a file >>

Additional Notes

Anonymous, October 28, 2004 12:07
you can also use the listchars option and set the trail character

this will use the SpecialKey syntax highlighting for trailing charaters.

:help listchars
Dubhead, October 28, 2004 19:22
I also hate redundant spaces, and had been highlighting them with a combination of 'highlight' and 'match' command.
But now I have switched to your 'au Syntax' approach.  This is simpler and better.  Thanks for the tip!

Here's a possible improvement that highlights only the redundant part:

au Syntax * syn match Error /\s\+$\| \+\ze\t/

'\ze' sets end of match.  See :he /ordinary-atom
Anonymous, November 3, 2004 21:01
I'm new to vim, using 6.3.16 and adding the above to my .vimrc doesn't seem to work for me.  Ideas?
Anonymous, November 4, 2004 8:56
The command "syntax on" should appear before the autocommand line.
If it is issued after, highlighting will not occur.

For gvim users, if there are any syntax commands inside of $VIM/gvimrc,
try inserting the autocommand line inside of the user gvimrc file instead,
as it is after the system gvimrc file.

-Frank.
mosh at albany, November 20, 2004 18:54
Here is how see the tabs in Makefiles

:au  FileType  make      set tabstop=8 noexpandtab list
pagaltzis@gmx.de, November 24, 2004 12:16
:au Syntax * syn match Error /\s\+$/ | syn match Error /\(^\s*\)\@<= \+\ze\t\+/

This will highlight just the spaces.
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 the maillist. Help Bram help Uganda.
   
Sponsored by Web Concept Group Inc. SourceForge.net Logo