Vim Tips Wiki
Advertisement

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Previous TipNext Tip

Tip: #396 - Highlight unwanted spaces

Created: January 9, 2003 19:04 Complexity: basic Author: Dubhead Version: 6.0 Karma: 147/80 Imported from: Tip#396

Whitespace characters (tabs, spaces, etc) at end of line are rarely meant to be there; they are usually there by accident. If you don't want them, maybe it pays to highlight them with an alarming color. (After all, GNU Emacs has it (show-trailing-whitespace), so why not in vim :-) )


Put this in your ~/.vimrc to highlight the whitespace characters at end of line:


highlight WhitespaceEOL ctermbg=red guibg=red

match WhitespaceEOL /\s\+$/

Comments

set list
<Look at trailing spaces and tabs> 
set nolist

< Now get rid of them>

%s/\s*$//g


Anonymous , January 9, 2003 22:13


You can also add this line to .vimrc:

let c_space_errors=1

This wil enable trailing whitespace errors in C/C++ files only.

xburgerhout at freeler dot nl , January 9, 2003 23:52


I prefer

set list listchars=tab:>-,trail:.,extends:> 


mgedmin--AT--mail.lt , January 10, 2003 4:55


You can also use the Cream-based 'showinvisibles' script, which puts bullets for every whitespace at the end of a line

falcon611--AT--yahoo.com , January 11, 2003 3:57


I run into the problem where some of the files I use have spaces instead of tabs at the front of the line. I have this in my .vimrc now:

<pre> " Check for extra whitespace highlight WhitespaceEOL ctermbg=red guibg=red match WhitespaceEOL /\s\+$/ match WhitespaceEOL /^\ \+/ </pre>

vim--AT--linuxwebguy.com , April 4, 2003 11:02


Here is a revision. In addition to whitespaces at end-of-line, this also highlights spaces before a tab.

" Highlight redundant whitespaces. highlight RedundantSpaces ctermbg=red guibg=red match RedundantSpaces /\s\+$\| \+\ze\t/


Dubhead , April 20, 2003 19:21


Hey...

The Highlight redundant whitespaces didn't work for me....

Following are the things I added to my rc file from this tip:

" Highlighting whitespaces at end of line highlight WhitespaceEOL ctermbg=red guibg=red match WhitespaceEOL /\s\+$/

" Check for extra spaces instead of tabs at the front of the line match WhitespaceEOL /^\ \+/

" Highlight redundant whitespaces. highlight RedundantSpaces ctermbg=red guibg=red match RedundantSpaces /\s\+$\| \+\ze\t/



suraj_amin--AT--yahoo.com , May 2, 2003 0:07


Hey...

The Highlight redundant whitespaces didn't work for me....

Following are the things I added to my rc file from this tip:

" Highlighting whitespaces at end of line highlight WhitespaceEOL ctermbg=red guibg=red match WhitespaceEOL /\s\+$/

" Check for extra spaces instead of tabs at the front of the line match WhitespaceEOL /^\ \+/

" Highlight redundant whitespaces. highlight RedundantSpaces ctermbg=red guibg=red match RedundantSpaces /\s\+$\| \+\ze\t/



suraj_amin--AT--yahoo.com , May 2, 2003 0:07


Sorry for the multiple post. Happened by mistake :(

suraj_amin--AT--yahoo.com , May 2, 2003 0:10


thanks mgedmin--AT--mail.lt

listchars was really what i was looking for whitespace lang since its better than syntax highlighting blocks



xpto_xpto--AT--sapo.pt , December 13, 2003 8:16


btw sounds the best for me set list listchars=tab:\|_,trail:.

xpto_xpto--AT--sapo.pt , December 13, 2003 8:23


Hi,

If you are using c_space_errors=1 to show the trailing spaces, be sure to use a color scheme that will display it.

I was using the Koehler color scheme and it couldn't display the trailing spaces.

-)

Frodak

frodak17--AT--hotmail.com , October 2, 2004 9:00


To have it only match when the cursor isn't at the end of the line:

hi WhiteSpaceEOL ctermbg=red
match WhiteSpaceEOL /\s\+\%#\--AT--!$/

Then you don't get red appearing all the time as you type, only when your cursor moves from the line any trailing whitespaces are exposed.

n0dalus , September 13, 2005 0:57


The suggestion by n0dalus didn't seem to take effect for me; to have vim match only when the cursor isn't at the end of the line, I settled on:

match WhitespaceEOL /\s\+\%#\@<!$/

kate, September 7, 2007 14:08


Advertisement