9

I am using vim 7.4.52 in Ubuntu 14.04 LTS.My vim background color is darkblue. I set this in .vimrc using set background darkblue

Now I want to set the background color with another color using hex code.

Is there any system to set this? i.e.set background=#300A24(this is my default terminal color)

3
  • 1
    :h 'background' :h hl-Normal Sep 30, 2016 at 9:45
  • 1
    @Christian Brabandt, this means what?
    – alhelal
    Oct 2, 2016 at 11:30
  • 1
    It means to read the help Oct 2, 2016 at 13:20

3 Answers 3

8

To set the background (and foreground) to some explicit color, you probably want to use a highlight command in your vimrc, such as

" plain text
hi Normal     ctermfg=227 ctermbg=17

(assuming you are indeed in a terminal and not a gvim window).

The color codes '227' and '17' are interpreted in the terminal, and, sadly, they are not simple hex codes. To find which colors they correspond to in a given terminal, you can use this little script:

#!/usr/bin/bash   
for i in {0..255} ; do
  printf "\x1b[38;5;${i}mcolour${i} "
done
1
  • 2
    I think that an important point to add to your answer is that the command used by OP was nonsensical: From :h 'background' you can see that background can only have two values light or dark, the doc says Setting this option does not change the background color, it tells Vim what the background color looks like.. So using background is not meant to change the background color.
    – statox
    Sep 30, 2016 at 11:12
2

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.

0

Go to your terminal Preferences, choose your preference profile (Unnamed by default), go to Colors and change the hex code for your background from there.

Picture below shows where this option is:

enter image description here

2
  • 3
    Welcome to Vi and Vim! This doesn't really have anything to do with vim nor changing vim's background color.
    – D. Ben Knoble
    Jun 9, 2021 at 16:57
  • I thought the OG question is about changing the background colour of Vim, that's why I answered with what I know.
    – Melon Eusk
    Jun 11, 2021 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.