开发者

How to properly extend a highlighting group in Vim?

开发者 https://www.devze.com 2023-02-14 22:21 出处:网络
I want to create a group named Italic to be exactly like Normal but with text in italic. My Normal group is

I want to create a group named Italic to be exactly like Normal but with text in italic. My Normal group is set to

Normal   ctermfg=251 ctermbg=234 guifg=#cccccc guibg=#242424

My questions are:

  1. Is the right way to do it is to add term=italic to the settings as follows?

    hi Italic term=italic ctermfg=251 ctermbg=234 guifg=#cccccc guibg=#242424
    
  2. I want to do it in a generic fashion, i.e.开发者_JS百科, define Italic such that the setting works for all colorschemes (the above will work only for my current colorscheme). Is there a way accomplish it? Something like

    hi Italic extends Normal term=italic 
    


To solve this issue you can create the highlighting group by script. The function below takes three string arguments: name of the group to base highlighting on, name of the group to create, and string containing additional highlighting properties (or properties to overwrite).

function! ExtendHighlight(base, group, add)
    redir => basehi
    sil! exe 'highlight' a:base
    redir END
    let grphi = split(basehi, '\n')[0]
    let grphi = substitute(grphi, '^'.a:base.'\s\+xxx', '', '')
    sil exe 'highlight' a:group grphi a:add
endfunction

Thus, the call

:call ExtendHighlight('Normal', 'Italic', 'term=italic')

creates a new group called Italic that extends Normal highlighting by the term=italic attribute string.

Note that custom highlighting groups remain unchanged on color-scheme switching. To correct this behavior you can update the group when the current color-scheme changes.

:autocmd ColorScheme * call ExtendHighlight('Normal', 'Italic', 'term=italic')
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号