7

I use CentOS 7 which has VIM 7.4 installed. By default the background property of VIM here is set to light irrespective of the colourscheme of my terminal. Although I can always force the desired background property in my vimrc, I do not want this.

In Ubuntu (18.04 which has VIM 8), background property automatically changes between light and dark based on the colourscheme of my terminal. This is true even when I am ssh-ing to the Ubuntu machine from my CentOS machine.

Therefore, I would like to know whether the ability of VIM changing background property automatically is specific to newer versions. And if this is not true, then how do I achieve the same effect in CentOS?

2
  • In CentOS, term=xterm-256color; $COLORFBGB is not set; third command gives E121: Undefined variable: v:termrbgresp and E15: Invalid expression: v:termrbgresp; and last command gives E846: Key code not set: t_RB.
    – Ombrophile
    Aug 1, 2018 at 10:44
  • In Ubuntu, term=xterm-256color; $COLORFGBG is not set; third command gives ^[]11;rgb:3232/3030/2f2f^G; and output of last command is t_RB=^[]11;?^G.
    – Ombrophile
    Aug 1, 2018 at 10:44

1 Answer 1

2

Vim has a few mechanisms for detecting the background colour of your terminal, which are described in :help 'background':

When the |t_RB| option is set, Vim will use it to request the background color from the terminal. If the returned RGB value is dark/light and 'background' is not dark/light, 'background' will be set and the screen is redrawn. This may have side effects, make t_BG empty in your .vimrc if you suspect this problem. The response to |t_RB| can be found in |v:termrbgresp|.

[...]

For MS-DOS, Windows and OS/2 the default is "dark". For other systems "dark" is used when 'term' is "linux", "screen.linux", "cygwin" or "putty", or $COLORFGBG suggests a dark background. Otherwise the default is "light".

By looking at the repository history, it seems as though the mechanism using a terminal query/response was first added in version 7.4.757 in 2015, although it looks as though it was still being tweaked as late as version 8.0.1129, in 2017.

If your version of Vim is older than that, then you should be able to get Vim to detect the background by setting an appropriate value of COLORFGBG in your shell. (e.g. 7;0. The first number is the foreground, the second is the background. Vim sets 'background' to dark if the second value is 0-6 or 8.) This variable is automatically set by some shells, and it looks like Vim has been using it for background-detection since around 2011.

More details on how Vim (and other apps) detect background colours can be found in the answers to this question on the Unix Stack Exchange.

3
  • Thank you. It seems that CentOS 7 has vim 7.4 (2013) version. In this t_RB setting is not available. Additionally, as you said , settings COLORFGBG seems to work, i.e., vim then automatically sets the background property. But that does not solves the problem. I now have to set COLORFGBG environment variable instead of setting background value in vimrc. Either way, the effort seems to be the same when I intend to change the terminal colour scheme.
    – Ombrophile
    Aug 2, 2018 at 19:44
  • .vimrc example possible for new vim users? The "classic" .vimrc that I found do not contain a t_RB... Thx
    – GGleGrand
    Feb 13, 2020 at 11:41
  • @GGleGrand If your terminal is configured correctly, t_RB will be set up automatically on Vim startup: you don't need to add it to your .vimrc.
    – Rich
    Feb 13, 2020 at 11:53

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.