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.
term=xterm-256color
;$COLORFBGB
is not set; third command givesE121: Undefined variable: v:termrbgresp
andE15: Invalid expression: v:termrbgresp
; and last command givesE846: Key code not set: t_RB
.term=xterm-256color
;$COLORFGBG
is not set; third command gives^[]11;rgb:3232/3030/2f2f^G
; and output of last command ist_RB=^[]11;?^G
.