[moon] home

Erlkönig: Xterm color queries

[parent webpage]

[webserver base]

[search erlkonig webpages]

[import certificates]


Xterm color queries in general

For querying an xterm for its current color settings, the relevant section of the ctlseqs.txt file follows. 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.

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 name 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.


Ps = 5 ; c ; spec -> Change Special Color Number c to the color specified by spec. This can be a name or RGB specifica- tion as per XParseColor. Any number of c name pairs may be given. The special colors can also be set by adding the maxi- mum number of colors to these codes in an OSC 4 control:

[...] (Similar info about using "?" instead of spec)

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\\"



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, and there's a chance they were set by the user, and so won't reflect the defaults with/without -rv. Two obvious possibilities are:

  • That users shouldn't use -rv to get dark backgrounds, but rather set them (via X resources, for example) as well as the basic palette choices. This conflicts with those few programs that heavily use color to present crude images, however.
  • There might be a way to determine whether reverse video is set, in which case the program running inside the xterm can adapt, rather than xterm itself. Ideal, since the demands of highlighting and other uses of color are disparate.

Querying xterm's background from a shell script

Some tweaking of the tty is required to do querying from the shell: (based on a script from http://invisible-island.net/xterm/xterm.faq.html). Here's an example in a toy script named xterm-bg

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
if IFS=';' read -r -d '\' color ; then
    result=$(echo $color | sed 's/^.*\;//;s/[^rgb:0-9a-f/]//g')
stty $oldstty
echo $result

Demonstration in an xterm with a black background:

$ ./xterm-bg

Demonstration in an xterm with a dark red background (xterm -bg rgbi:.3/0.0):

$ ./xterm-bg
encrypt lang [de jp fr] diff backlinks (sec) validate printable
Walk without rhythm and you won't attract the worm.
[ Your browser's CSS support is broken. Upgrade! ]
alexsiodhe, alex north-keys