38

Is there an easy way to display whitespace characters such as space and tab in gvim? Something like what is implemented in Gedit, Geany, Komodo, and other GUI editors where (when the option is turned on) spaces show as a muted or greyed-out '.' and tabs as '-->'.

2
  • All good answers... but I don't want to high-light the spaces; I've seen themes with that turned on and honestly they all look fairly ugly (to me). I'll do some more experimenting with the list/listchars options and see if I can't find something a little closer to what I have in mind.
    – memilanuk
    Feb 15, 2011 at 23:34
  • Same as Make Vim show ALL white spaces as a character. You could use the recently updated patch for Vim in this mailing list thread.
    – glts
    Nov 10, 2013 at 12:47

4 Answers 4

39

Check out listchars and list options in Vim. An example use of this feature:

" part of ~/.vimrc
" highlight tabs and trailing spaces
set listchars=tab:>-,trail:-
set list
6
  • 1
    So far I'm using the 'set listchars...' bit from your post and the noremap command from the post by UncleZ... they seem to work well together. Is there a way to get the spaces look like '--->' instead of '>-' with trailing '-'? I tried simply substituting the two strings but that didn't work very well :/
    – memilanuk
    Feb 15, 2011 at 23:45
  • Unfortunately I don't think it's possible in Vim. Feb 16, 2011 at 14:42
  • @memilanuk: It always confused me that it's '>-------'. If you found a solution, please post it. Jan 7, 2012 at 7:40
  • No such luck. Seems that two of my favorite 'visual' aids - 'show whitespace' and 'indent guides' which most GUI editors or IDEs implement are not doable in vim/gvim as is.
    – memilanuk
    Jan 7, 2012 at 19:41
  • @LudwigWeinzierl The reason it shows ">-------" is because your tabstop (width to display your tab char) is set to 8 (1 arrow, 7 hyphens). Set tabstop=4 and it will be ">---". Nov 29, 2012 at 6:19
30

You can use any characters you wish if you enable Unicode first

set encoding=utf-8

One line I use (put in ~/.vimrc):

set list listchars=tab:→\ ,trail:·

Learn more about this setting at http://vim.wikia.com/wiki/Highlight_unwanted_spaces

The color of these characters is controlled by your color scheme.

7

Here are some of my settings pertaining whitespace.

Use F11 to toggle between displaying whitespace characters or not:

noremap <F11> :set list!<CR>

How to show whitespace characters when list is set:

set listchars=eol:$,tab:>-,trail:.,extends:>,precedes:<,nbsp:_

Highlight special characters in yellow:

highlight SpecialKey term=standout ctermbg=yellow guibg=yellow

Highlight redundant spaces (spaces at the end of the line, spaces before or after tabs):

highlight RedundantSpaces term=standout ctermbg=Grey guibg=#ffddcc    
call matchadd('RedundantSpaces', '\(\s\+$\| \+\ze\t\|\t\zs \+\)\(\%#\)\@!')

Hope these help!

2
  • When adding the last bit, I get: E28: No such highlight group name: RedundantSpaces Nov 29, 2012 at 6:16
  • Hi @UncleZeiv Please edit your answer in order to swap the two last lines (highlight before matchadd) as you suggested in your last comment. Cheers
    – oHo
    Feb 5, 2015 at 17:31
0

This works well for me:

"trailing white space detection
highlight WhitespaceEOL ctermbg=yellow guibg=yellow
match WhitespaceEOL /\s\+$/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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