开发者

Is there a way to determine if a a highlight has already been defined in Vim?

For a filetype plug-in, I would like to define and use a default custom highlight name, e.g.:

hi CsvColumn guifg=black guibg=NavajoWhite ctermfg=black ctermbg=yellow

However, I would like to allow users to define their own in preference to the default, so to do something like:

if <somehow or 开发者_JS百科other check if 'CsvColumn' has NOT been defined>
    hi CsvColumn guifg=black guibg=NavajoWhite ctermfg=black ctermbg=yellow
fi

Is there a way to do this?

Since this is in a filetype plugin, it gets executed once everytime a buffer is loaded or read, so presumably there is a good chance it will be sourced after the user's '.vimrc' has been sourced, so any definition the user provides there or in a colorscheme will be overridden if I do not do the check.


There is a function to do that, called hlexists({name}):

The result is a Number, which is non-zero if a highlight group called {name} exists. This is when the group has been defined in some way. Not necessarily when highlighting has been defined for it, it may also have been used for a syntax item.

So your code would be:

if hlexists('CsvColumn')
    hi ...
endif
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜