Show whitespace characters in gvim

2020-02-16 07:22发布

问题:

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

回答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


回答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.



回答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!



回答4:

This works well for me:

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