Show whitespace characters in gvim

33,960

Solution 1

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

Solution 2

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.

Solution 3

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!

Share:
33,960
memilanuk
Author by

memilanuk

Updated on October 28, 2020

Comments

  • memilanuk
    memilanuk over 3 years

    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
    memilanuk about 13 years
    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 :/
  • Marek Sapota
    Marek Sapota about 13 years
    Unfortunately I don't think it's possible in Vim.
  • Ludwig Weinzierl
    Ludwig Weinzierl over 12 years
    @memilanuk: It always confused me that it's '>-------'. If you found a solution, please post it.
  • memilanuk
    memilanuk over 12 years
    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.
  • Shane Reustle
    Shane Reustle over 11 years
    When adding the last bit, I get: E28: No such highlight group name: RedundantSpaces
  • Shane Reustle
    Shane Reustle over 11 years
    @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 ">---".
  • oHo
    oHo about 9 years
    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
  • Olivier Dulac
    Olivier Dulac about 2 years
    @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)