Extending a highlighting group in Vim

2020-06-23 07:34发布

I want to create a group-name Italic that is exactly like Normal but with text in italic. My group-name Normal 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., I want to define Italic such that the setting works for all colorscheme (the above will work only for my specific colorscheme). Is there a way accomplish it? something like

    hi Italic extends Normal term=italic 
    

1条回答
ら.Afraid
2楼-- · 2020-06-23 07:57

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')
查看更多
登录 后发表回答