Closed AgustinOrdonez closed 1 year ago
Hi there,
for general Vim and Neovim topics the manual is the best source, see :h :hightlight
. As to where to put that code, you can put it anywhere you want in your configuration. Either your init.vim
or some plugin/whatever.vim
file. The init.vim
and any file in plugin
will be sourced at startup. Personally I have a plugin/rainbow.vim
file which has settings for all my rainbow settings, but that's completely up to you.
If you want to use Lua there is no dedicated Lua API for highlighting. You have to use either Lua's Vim script bridge (:h lua-vim
) or the Neovim API (:h api
).
-- Call a Vim script as a string
vim.cmd 'highlight RainbowDelimiterOrange guifg=#d65d0e ctermfg=White'
-- Call Vim script as a table
vim.cmd {cmd = 'highlight', args = {'RainbowDelimiterOrange', 'guifg=#d65d0e', 'ctermfg=White'}}
-- Using the Neovim API
vim.api.nvim_set_hl(0, 'RainbowDelimiterOrange', {fg = '#d65d0e', ctermfg= 'White' })
They all do the same, so it is up to you which one you prefer. If you want to generate the command prefer the latter two instead of trying to glue together a string.
local colours = {
RainbowRed = '#FF0000',
RainbowGreen = '#00FF00',
RainbowBlue = '#0000FF'
}
for name, code in pairs(colours) do
-- Bad, this is fragile
vim.cmd(string.format('highlight %s guifg=%s', name, code))
-- Better, we only glue together one string
vim.cmd {cmd = 'highlight', args = {name, 'guifg=' .. code}}
-- Best, no string interpolation at all
vim.api.nvim_set_hl(0, name, {fg = code})
end
Of course for something this simple it does not really matter, but it is worth keeping in mind for when you want to call more complicated command.
Thank you so much for this detailed answer! Very useful.
I attempted to modify the highlight groups in this manner, putting the vim.api.nvim_set_hl(...
command both before and after my call of vim.g.rainbow_delimiters = {...
and it didn't have any effect, presumably because I set my theme later in my config which overwrites anything here.
So for me, I had to define the highlight group after setting my theme. Probably obvious to some people, but just putting this here in case it helps someone.
Themes usually have a line of code near the beginning that clears all highlight groups, so if you define your custom groups before you set your theme your changes will be wiped out. See :h :hi-clear
.
Hello, I am new to the whole vim ecosystem. I've read on the documentation I could redefine highlight groups with the following:
But I fail to understand where should I put that. Also I guess this example is written in vimscript since comments start with ", how could I do this on lua?