views:

103

answers:

2

I recently saw this demo. My question is: how is it possible in vim to show whitespace characters under the cursor as dots (in red for example).

+7  A: 

I don't think that's showing space under the cursor; I think it's showing trailing spaces. This is controlled by the listchars option, which controls the behavior of list. To turn it on, use:

set list
set listchars=trail:·

The color is determined by the hl-SpecialKey group; you can change it like this:

hi SpecialKey ctermfg=Red
Jefromi
I see, can I still show whitespace near the cursor?
akosch
The intent of `listchars` is to let you distinguish types of whitespace - you can show end of line, tab, trailing whitespace, indicators of more text to left and right of the screen, and nonbreaking spaces. Plain spaces are left blank, because they're common, and with all the other things highlighted, anything blank is a space. The upshot: there's no way to show plain spaces everywhere, and there's certainly no way to apply `list` only "near" the cursor.
Jefromi
+1  A: 

Highlight whitespace before and after the cursor

highlight FooBar guibg=#80a0ff
au CursorMoved * :match FooBar /\s*\%#.\s*/

That works for normal mode with gvim. To make it work in terminal vim change the guibg to ctermbg. If you want it also in insert mode, change au CursorMoved to

au CursorMoved,CursorMovedI,InsertEnter

Although in that case you'd need to tweak the regex since that one matches erroneously non-whitespace characters to the right of the cursor when entering insert mode.

In the regex \%# matches the current cursor position. I had to use . after it since it wouldn't match whitespace to the right of the cursor without it.

Heikki Naski