Reputation: 19333
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:
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:
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:
Upvotes: 10
Views: 10453
Reputation:
(Sorry I just want to post a solution for neovim from 2024 but I don't have enough reputation to comment under rodrigo's answer...!)
Inspired by rodrigo's answer:
vim.cmd([[2match WhiteSpaceBol /^ \+/]])
vim.cmd('match WhiteSpaceMol / /')
vim.api.nvim_set_hl(0, 'WhiteSpaceMol', {
fg = string.format('#%x', vim.api.nvim_get_hl(0, { name = 'Normal' }).bg)
})
When switching between coloschemes, it's not suffice. So register a autocmd to handel it:
vim.cmd([[2match WhiteSpaceBol /^ \+/]])
vim.cmd('match WhiteSpaceMol / /')
vim.api.nvim_set_hl(0, 'WhiteSpaceMol', {
fg = string.format('#%x', vim.api.nvim_get_hl(0, { name = 'Normal' }).bg)
})
vim.api.nvim_create_autocmd('ColorScheme', {
callback = function()
vim.api.nvim_set_hl(0, 'WhiteSpaceMol', {
fg = string.format('#%x', vim.api.nvim_get_hl(0, { name = 'Normal' }).bg)
})
end
})
The only issue here is the space will still distinct if cursorline has different color from Normal
Upvotes: 1
Reputation: 98358
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.
Upvotes: 11