While Dalker's answer is technically correct, it's not entirely accurate, at least not anymore - the exact answer to this depends on your terminal emulator.
Highlighting backgrounds is general is done by applying a background highlight (ctermbg
or guibg
) to the Normal
highlight group. See :h background
and this answer on SO.
For terminals that only support 256 colors, that answer is completely correct. The specific codes for various colors is, as far as I know, influenced by a palette set by the terminal, and that does include the default background color.
This is also why Melon Eusk's answer does work if you're stuck on 8/256 colors, at least provided you know which color code it changes.Obligatory disclaimer: when it comes to the ANSI color palette, I'm slightly out of my depth technically. I have no idea why terminals, or at least mine (Gnome Terminal) has 16 colors for customization, but somehow still manages to spew out a 256 color palette. TL;DR: don't ask me why, I have no idea. Admittedly, this only works if you're trying to change your current terminal colors to something else, and not if you want Vim to match your terminal background.
If, however, you're on a terminal emulator that supports true color (24 bit color), you can actually use hex codes - highlight commands can also use a combination of cterm
and gui
to support both 256 colors and true color. The best part about doing a hybrid solution is that Vim figures it out for you - at least as long as you set termguicolors
.
Checking support
First and foremost, if you don't has("termguicolors")
, you cannot haz GUI colors. If your terminal supports it, you'll have to (re)compile Vim and make sure the feature is enabled in Vim itself. I believe the has
should work regardless of whether the terminal supports it as long as Vim has built-in support for it, but I'm not entirely sure. I don't have access to a 256 color terminal without truecolor I can test in either.
Of course, if you want to use termguicolors
, you need a terminal that supports it. According to :h 'termguicolors'
, Windows users in the win32 console have true color from Win10 1703 and up, which can be verified with has('vcon')
.
On UNIX (or at least Linux), it should be enough to check if echo $COLORTERM
is "truecolor" or "24bit".
That being said, I personally struggle finding terminals that don't support true color for testing, so as long as you stay up-to-date, odds are that you're running a true color terminal right now. Also worth noting that both the above-mentioned checks can also be done in vimscript (admittedly, the one checking $COLORTERM
requires a small rewrite to vimscript) and make the next bit portable. Note that this isn't necessarily portable over SSH, WSL (can't test), or if you in any other way "leave" your own system and do a remote connection, in which you start Vim. Portability over different systems is, in that regard, pretty tricky, and may require additional hacking to get it to work. I digress.
See also :h xterm-true-color
and this gist it links to, which includes a list of terminals that support true color.
Enabling and using true color
As I've already mentioned earlier, all you need to do at this point is:
set termguicolors
And then, highlight:
hi Normal guibg=#300A24
You can also throw in ctermbg=whatever code
as outlined in Dalker's answer - which Vim decides to use (when you're in a terminal, anyway) depends on whether you've set termguicolors
or not. You can also try highlighting both and watch it flicker back and forth by toggling termguicolors
, if you want to observe this in practice.
Note that this may need to come in addition to a set background=dark
prior to the highlight, depending on whether or not you override a colorscheme, etc. This at least applies if you're changing from a light background to the one in question. Either that or it's just a slip-up while testing this in a live instance of Vim - your mileage may vary.
TL;DR:
In general, in order to use hexadecimal codes for highlighting, you need to use a gui*
highlighting instruction; either guifg
or guibg
. These do work identically to ctermfg
and ctermbg
respectively, but they support true color (and I don't think they support terminal ANSI colors by number).
For these to work in terminal Vim, you additionally need a terminal that supports true color, Vim that supports +termguicolors
, and to set termguicolors
to actually make Vim use colors specified with guifg
and guibg
.
There's also guisp
, but the documentation indicates that it's used for undercurl and strikethrough, and not normal highlighting.
:h 'background'
:hhl-Normal