Is there a way to update highlight groups with Lua API

I want to remove the background of an existing highlight group using Lua API. It seems that nvim_set_hl completely replaces the definition (as from the help docs below). Does this mean that I’ll have to use nvim_get_hl_by_name to fetch the current attributes and update those before passing them into nvim_set_hl? Just wondering if this is what the Vimscript :highlight does as well and whether there are better solutions to this.

 Note: Unlike the `:highlight` command which can update a
                highlight group, this function completely replaces the
                definition. For example: `nvim_set_hl(0, 'Visual', {})` will
                clear the highlight group 'Visual'.

There are ways to do it.

Just use vim.cmd('highlight ...')

or

Use vim.api.nvim_get_hl_by_name to get the existing definition. (this won’t return separate cterm and gui colors though) and then rebuild the highlight from it.

or

Use vim.api.nvim_get_hl_id_by_name to get the id from the name then use vim.fn.synIDattr to get each attribute with that id and rebuild the highlight from them.

1 Like