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 '-->'.

memilanuk
  • 3,522
  • 6
  • 33
  • 39
  • 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 '11 at 23:34
  • Same as [Make Vim show ALL white spaces as a character](http://stackoverflow.com/q/1675688). You could use the recently updated patch for Vim in [this mailing list thread](https://groups.google.com/d/msg/vim_dev/dIQHjW1g92s/FPUU_-9N3wYJ). – glts Nov 10 '13 at 12:47

4 Answers4

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
Marek Sapota
  • 20,103
  • 3
  • 34
  • 47
  • 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 '11 at 23:45
  • Unfortunately I don't think it's possible in Vim. – Marek Sapota Feb 16 '11 at 14:42
  • @memilanuk: It always confused me that it's '>-------'. If you found a solution, please post it. – Ludwig Weinzierl Jan 07 '12 at 07: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 07 '12 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 ">---". – Shane Reustle Nov 29 '12 at 06:19
  • @memilanuk: you could : `set listchars=tab:<->,trail:-` # this will mark tabs as: <-> <--> <----> etc. ie, tab: accepts: "ab" or "abc" characters (b being expanded as needed) – Olivier Dulac Feb 01 '22 at 14:36
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.

vgoff
  • 10,980
  • 3
  • 38
  • 56
wuputah
  • 11,285
  • 1
  • 43
  • 60
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!

UncleZeiv
  • 18,272
  • 7
  • 49
  • 77
  • When adding the last bit, I get: E28: No such highlight group name: RedundantSpaces – Shane Reustle Nov 29 '12 at 06: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 05 '15 at 17:31
0

This works well for me:

"trailing white space detection
highlight WhitespaceEOL ctermbg=yellow guibg=yellow
match WhitespaceEOL /\s\+$/
Ry-
  • 218,210
  • 55
  • 464
  • 476
tony
  • 1