Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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.

like image 379
Jeet Avatar asked Sep 05 '25 03:09

Jeet


1 Answers

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
like image 56
UncleZeiv Avatar answered Sep 08 '25 06:09

UncleZeiv