GNOME Bugzilla – Bug 733423
Please set COLORFGBG environment variable for automatic color detection
Last modified: 2017-03-13 13:02:55 UTC
Various terminals, including urxvt and konsole, set an environment variable "COLORFGBG" to allow applications to detect the foreground and background colors. Various programs, such as vim, use this to determine whether to use a color scheme that works best on a light or dark background. Please consider setting this variable in gnome-terminal as well. Semantics: COLORFGBG should contain two fields separated by a semicolon, representing the foreground and background colors, respectively. Each one should consist of a number from 0-15 if that color matches color 0-15, or "default" otherwise.
Out of quite a few terminals I've just tried on linux, only urxvt and konsole set it (the ones that don't: xterm, st, terminology, pterm). Konsole and Urxvt use different syntax and semantics, i.e. for me konsole sets it to "0;15" (even though I use the "Black on Light Yellow" color scheme - so why not "default" instead of "15"?), whereas my urxvt sets it to "0;default;15" (it's actually black on white - but why three fields?). So in neither of these two does the value match your specification. I'd argue that COLORFGBG is broken by design. First, the value of "default" according to your specification would be completely useless in case the bg or fg color doesn't exactly match the white or black palette colors (and using the word "default" is really unfortunate). Second, there's absolutely no way to update the env variable upon profile change (or runtime change of the palette, either via settings or via escape sequences). Third, I _guess_ its value is not forwarded by ssh and friends. Instead, I recommend using the OSC 4/10/11 escape sequences, which are already supported by vte/gnome-terminal. You're free to write a .bashrc or equivalent that queries the actual colors using these escapes and sets COLORFGBG accordingly, or a wrapper in front of vim that sets this. You can probably even put this in the shell code that shows the prompt, resulting in the variable being updated soon after a profile change - something g-t couldn't do. Also, I'd consider asking vim developers to add support for OSC 4/10/11 querying the color, in addition to checking COLORFGBG.
Here's an example .bashrc fragment, feel free to improve as you wish. if [ -n "$VTE_VERSION" -a -z "$COLORFGBG" ]; then stty -echo echo -ne '\e]10;?\a\e]11;?\a' IFS=: read -t 0.1 -d $'\a' x fg IFS=: read -t 0.1 -d $'\a' x bg if [[ ${fg:0:1} =~ [01234567] ]]; then export COLORFGBG='0;15' else export COLORFGBG='15;0' fi unset x fg bg stty echo fi
You can also use xtermcontrol --get-fg xtermcontrol --get-bg etc.
Egmont is right; WONTFIX.