10

Is it possible in Vim to have my editor (when editing .c and .h files), show via listchars, a special character only for leading space characters?

I found a separate post that noted, as of version 7.4, Vim now supports highlighting all space characters via listchars. Here's my current listchars variable:

set list listchars=tab:>-,trail:.,extends:>,precedes:<,space:.

And here is a render of how it appears on my screen:

This it appear on my screen

However, I would like it to appear like so (below), where only leading spaces are rendered via listchars, and spaces occurring after indentation-related spaces are not rendered. ie:

The desired behaviour

Is there a simple way to accomplish this, either via color scheme or .vimrc changes?


Image diff in case the difference isn't obvious due to low contrast:

Image diff in case the difference isn't obvious due to low contrast

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
Cloud
  • 18,753
  • 15
  • 79
  • 153

1 Answers1

11

I don't think that linechars will help you, but this highlight might help:

highlight WhiteSpaceBol guibg=lightgreen
match WhiteSpaceBol /^ \+/

Change the color scheme for whatever you like best.

If you insist on having the fancy · you can get them with a bit of a hack:

set listchars=space:·
highlight WhiteSpaceBol guifg=blue
highlight WhiteSpaceMol guifg=white
match WhiteSpaceMol / /
2match WhiteSpaceBol /^ \+/

Now, only the starting · are visible! (change the white for whatever color you use as background and blue with the color of your choice).

NOTE: If you use the console Vim, replace (or add) guibg with ctermbg and the proper colors.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • Having some issues with this. Do all of the above go into `.vimrc`? – Cloud Nov 09 '16 at 21:10
  • 1
    Yes, I've but those in `.vimrc` (version 8.0) and it worked with `gvim`. With the `vim` console you should use `ctermbg` instead of `guibg`. – rodrigo Nov 09 '16 at 21:58
  • @DevNull: Can you be more specific? What are your issues? – rodrigo Nov 09 '16 at 21:58
  • I put the entire list (all 7 lines above) in my VIMRC, but the non-leading `space` characters are still visible, using the same light-grey color as "leading" `space` characters (ie: rendered output on-screen is the same before-and-after adding the 7 lines of code you posted). Maybe my existing color scheme settings are interfering with it. – Cloud Nov 09 '16 at 22:22
  • @DevNull Are you using console Vim or Gvim? – rodrigo Nov 09 '16 at 22:27
  • Console vim built from Git source, plus I enabled `xterm_clipboard` support. – Cloud Nov 09 '16 at 22:45
  • @DevNull And did you try with `ctermbg`? – rodrigo Nov 09 '16 at 22:47
  • Yep. Tried with `ctermbg`, no effect. I removed the following entry from my custom color scheme, but now, even with the above code, all spaces are bright blue. `hi SpecialKey guifg=#1c1c1c ctermfg=236 `. – Cloud Nov 09 '16 at 23:11
  • Nice! Note that you have to do this in your vimrc _after_ setting the colorscheme. Otherwise, the colorscheme will undo any custom highlighting – Jeff Irwin Mar 19 '23 at 18:41