Programmatic access to current xterm background color?
The email back from Thomas Dickey (xterm's maintainer) has this. Note in particular the part about ?
. The Ps = 4
refers to OSC Ps ; Pt ST
where OSC
(the "Operating System Control" prefix) is ESC ]
and ST
(the "String Terminator" suffix) is \
(backslash). The 4
is one of the possible subcommands to OSC.
For the whole palette, that can be set/retrieved using the 88/256 color extension. In ctlseqs.txt, it's summarized here:
Ps = 4 ; c ; spec -> Change Color Number c to the color specified by spec. This can be a name or RGB specification as per XParseColor. Any number of c/spec pairs may be given. The color numbers correspond to the ANSI colors 0-7, their bright versions 8-15, and if supported, the remainder of the 88-color or 256-color table. If a "?" is given rather than a name or RGB specification, xterm replies with a control sequence of the same form which can be used to set the corresponding color. Because more than one pair of color number and specification can be given in one control sequence, xterm can make more than one reply.
A bit later in the docs are more OSC subcommands, Ps = 10
and Ps = 11
, and others.
Ps = 1 0 -> Change VT100 text foreground color to Pt. Ps = 1 1 -> Change VT100 text background color to Pt.
Example - this queries the background using Ps = "11"
(from just above) and Pt = "?"
, plugged into the OSC Ps ; Pt ST
. In the echo, \033
is being used for escape, and \\
for the final backslash.
echo -en "\033]11;?\033\\"
Output:
^[]11;rgb:0000/0000/0000^[\
Warning: The returned color does not reflect whether reverse video, like -rv
, is enabled, and crawling through the ~260 colors available via OSC 4 ; c ; ? ST
doesn't show any that both follow the background AND change with reverse video. Since many users set a dark background by using just xterm -rv
, this complicates determining whether the background is actually dark or not. Most colors don't adjust to -rv
, either.
A script to do the full query and actually capture the reply from xterm:
#!/bin/bash
success=false
exec < /dev/tty
oldstty=$(stty -g)
stty raw -echo min 0
col=11 # background
# OSC Ps ;Pt ST
echo -en "\033]${col};?\033\\" >/dev/tty # echo opts differ w/ OSes
result=
if IFS=';' read -r -d '\' color ; then
result=$(echo $color | sed 's/^.*\;//;s/[^rgb:0-9a-f/]//g')
success=true
fi
stty $oldstty
echo $result
$success
kind of
put the settings into your ~/.Xdefaults file:
xterm*foreground: blue
xterm*background: white
in your shell you just grep the values:
awk '/xterm\*foreground:(.*)/ { print $2 }' < .Xdefaults
otherwise it's pretty hard to get some internal values of the xterm.