Vim Tips Wiki
m (Fixed formatting and links, set category, remove duplicate comments and rearranged the content a little)
(Change to TipImported template + severe manual clean)
Line 1: Line 1:
 
{{duplicate|721|811|1274|878|1487}}
 
{{duplicate|721|811|1274|878|1487}}
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=396
 
|id=396
  +
|previous=395
|title=Highlighting whitespaces at end of line
 
  +
|next=397
|created=January 9, 2003 19:04
+
|created=January 9, 2003
 
|complexity=basic
 
|complexity=basic
 
|author=Dubhead
 
|author=Dubhead
 
|version=6.0
 
|version=6.0
 
|rating=147/80
 
|rating=147/80
 
}}
|text=
 
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 :-) )
+
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.
  +
 
Put the following code in your vimrc file.
   
Put the below code is in your ~/.vimrc.
 
 
<pre>
 
<pre>
 
" Highlight redundant whitespaces
 
" Highlight redundant whitespaces
 
" This will highlight the whitespace characters at end of line
 
" This will highlight the whitespace characters at end of line
 
highlight WhitespaceEOL ctermbg=red guibg=red
 
highlight WhitespaceEOL ctermbg=red guibg=red
match WhitespaceEOL /\s\+$/
+
match WhitespaceEOL /\s\+$/
" This will highlight spaces before a tab:
+
" This will highlight spaces before a tab:
highlight RedundantSpaces ctermbg=red guibg=red
+
highlight RedundantSpaces ctermbg=red guibg=red
match RedundantSpaces /\s\+$\| \+\ze\t/
+
match RedundantSpaces /\s\+$\| \+\ze\t/
 
</pre>
 
</pre>
Dubhead
 
, April 20, 2003 19:21
 
}}
 
   
== Comments ==
+
==Comments==
To see trailing spaces and tabs, you can use:
+
To see trailing spaces and tabs, you can use:
 
<pre>
 
<pre>
 
:set list
 
:set list
 
:set nolist
 
:set nolist
 
</pre>
 
</pre>
  +
To get rid of them:
+
To get rid of them:
 
<pre>
 
<pre>
:%s/\s*$//g
+
:%s/\s*$//g
 
</pre>
 
</pre>
   
'''Anonymous'''
 
, January 9, 2003 22:13
 
 
----
 
----
You can also add this line to .vimrc:
+
You can also add this line to .vimrc:
 
<pre>
 
<pre>
let c_space_errors=1
+
let c_space_errors=1
 
</pre>
 
</pre>
  +
 
This wil enable trailing whitespace errors in C/C++ files only.
 
This wil enable trailing whitespace errors in C/C++ files only.
   
xburgerhout at freeler dot nl
 
, January 9, 2003 23:52
 
 
----
 
----
I prefer
+
I prefer
 
<pre>
 
<pre>
set list listchars=tab:&gt;-,trail:.,extends:&gt;
+
set list listchars=tab:&gt;-,trail:.,extends:&gt;
 
</pre>
 
</pre>
   
mgedmin--AT--mail.lt
 
, January 10, 2003 4:55
 
 
----
 
----
 
You can also use the Cream-based {{script|id=363|text=showinvisibles}} script, which puts bullets for every whitespace at the end of a line
 
You can also use the Cream-based {{script|id=363|text=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:
+
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>
 
<pre>
 
" Check for extra whitespace
 
" Check for extra whitespace
 
highlight WhitespaceEOL ctermbg=red guibg=red
 
highlight WhitespaceEOL ctermbg=red guibg=red
 
match WhitespaceEOL /\s\+$/
 
match WhitespaceEOL /\s\+$/
match WhitespaceEOL /^\ \+/
+
match WhitespaceEOL /^\ \+/
 
</pre>
 
</pre>
   
vim--AT--linuxwebguy.com
 
, April 4, 2003 11:02
 
 
----
 
----
 
The Highlight redundant whitespaces didn't work for me.
----
 
Hey...
 
   
 
Following are the things I added to my rc file from this tip:
The Highlight redundant whitespaces didn't work for me....
 
 
Following are the things I added to my rc file from this tip:
 
 
<pre>
 
<pre>
" Highlighting whitespaces at end of line
+
" Highlighting whitespaces at end of line
highlight WhitespaceEOL ctermbg=red guibg=red
+
highlight WhitespaceEOL ctermbg=red guibg=red
match WhitespaceEOL /\s\+$/
+
match WhitespaceEOL /\s\+$/
   
" Check for extra spaces instead of tabs at the front of the line
+
" Check for extra spaces instead of tabs at the front of the line
match WhitespaceEOL /^\ \+/
+
match WhitespaceEOL /^\ \+/
   
" Highlight redundant whitespaces.
+
" Highlight redundant whitespaces.
highlight RedundantSpaces ctermbg=red guibg=red
+
highlight RedundantSpaces ctermbg=red guibg=red
match RedundantSpaces /\s\+$\| \+\ze\t/
+
match RedundantSpaces /\s\+$\| \+\ze\t/
 
</pre>
 
</pre>
   
suraj_amin--AT--yahoo.com
 
, May 2, 2003 0:07
 
 
----
 
----
BTW, this sounds the best for me:
+
This sounds the best for me:
 
<pre>
 
<pre>
 
set list listchars=tab:\|_,trail:.
 
set list listchars=tab:\|_,trail:.
 
</pre>
 
</pre>
   
xpto_xpto--AT--sapo.pt
 
, December 13, 2003 8:23
 
 
----
 
----
 
If you are using c_space_errors=1 to show the trailing spaces, be sure to use a color scheme that will display it.
Hi,
 
   
 
I was using the Koehler color scheme and it couldn't display the trailing spaces.
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:
+
To have it only match when the cursor isn't at the end of the line:
 
<pre>
 
<pre>
:hi WhiteSpaceEOL ctermbg=red
+
:hi WhiteSpaceEOL ctermbg=red
:match WhiteSpaceEOL /\s\+\%&#35;\--AT--!$/
+
:match WhiteSpaceEOL /\s\+\%#\@!$/
 
</pre>
 
</pre>
  +
 
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.
 
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:
+
To have vim match only when the cursor isn't at the end of the line, I settled on:
 
<pre>
 
<pre>
match WhitespaceEOL /\s\+\%#\@<!$/
+
match WhitespaceEOL /\s\+\%#\@<!$/
 
</pre>
 
</pre>
   
 
----
kate, September 7, 2007 14:08
 
 
 
[[Category:Syntax]]
 
[[Category:Syntax]]

Revision as of 12:33, 2 November 2007

Duplicate tip

This tip is very similar to the following:

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

Tip 396 Printable Monobook Previous Next

created January 9, 2003 · complexity basic · author Dubhead · version 6.0


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.

Put the following code in your vimrc file.

" Highlight redundant whitespaces
" This will highlight the whitespace characters at end of line
highlight WhitespaceEOL ctermbg=red guibg=red
match WhitespaceEOL /\s\+$/
" This will highlight spaces before a tab:
highlight RedundantSpaces ctermbg=red guibg=red
match RedundantSpaces /\s\+$\| \+\ze\t/

Comments

To see trailing spaces and tabs, you can use:

:set list
:set nolist

To get rid of them:

:%s/\s*$//g

You can also add this line to .vimrc:

let c_space_errors=1

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


I prefer

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

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


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:

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

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/

This sounds the best for me:

set list listchars=tab:\|_,trail:.

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.


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

:hi WhiteSpaceEOL ctermbg=red
:match WhiteSpaceEOL /\s\+\%#\@!$/

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.


To have vim match only when the cursor isn't at the end of the line, I settled on:

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