Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

nano has a useful bit of syntax highlighting that actually highlights whitespace (tabs and spaces), under two conditions: (1) the whitespace does not have a non-whitespace character between either the last character or the beginning of the line and it, and (2) that the file is source code and not plain, plain text (as in a shopping list). How can I emulate this kind of behavior in vim?

share|improve this question
up vote 4 down vote accepted

I use set list and set listchars in .vimrc to show tabs and trailing white spaces, you can use a condition for selective file type like this.

if !(&filetype == "txt")
  set list                " show special characters
  set listchars=tab:→\ ,trail:·,nbsp:·

So my files look like this when those charaters are present.

function someFunc() { // no trailing spaces here
→   var a = "hola"; // 3 trailing spaces.···
    alert(a); // this line starts with spaces instead of tab
// next a line with 4 white spaces and nothing else
// next a line with a couple tabs
→   →   

Note: · is not .


So to answer to your comment, you can do that by adding this to your ~/.vimrc, make sure to add it after the colorscheme, or it will be hi clear'd.

if !(&filetype == "txt")
  highlight WhiteSpaces ctermbg=green guibg=#55aa55
  match WhiteSpaces /\s\+$/

You can change the highlight colors and refine the regular expression as needed. /\s\+$/ will match trailing spaces or tabs and lines that contain nothing but either of those 2 characters. If you only want to highlight lines with just tabs and spaces use /^\s\+$/ instead.

share|improve this answer
nano, I just realized, doesn't replace the tabs and spaces, but changes them to the "black/white on green" coloring, which makes them appear as green bars. Is there a way that I can do this instead of inserting a separate character? – InkBlend Jan 27 '13 at 23:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.