0

I want to change the Xterm background color based on what branch I'm currently in. Is there a good way to automate this?

Currently I'm changing the profile by hand. And I find it a bit annoying that google couldn't provide me with an answer. Everyone seems settled by just changing the prompt.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
user463923
  • 105
  • 1
  • 5

1 Answers1

1

You could start with "fixing the prompt": some of those answers deal with modifying the (bash or zsh) prompts to send escape sequences which change the window title. The background color for xterm would be just another escape sequence, part of the same group of Operating System Commands:

OSC Ps ; Pt BEL
OSC Ps ; Pt ST
          Set Text Parameters.  For colors and font, if Pt is a "?", the
          control sequence elicits a response which consists of the con-
          trol sequence which would set the corresponding value.  The
          dtterm control sequences allow you to determine the icon name
          and window title.

...

          The 10 colors (below) which may be set or queried using 1 0
          through 1 9  are denoted dynamic colors, since the correspond-
          ing control sequences were the first means for setting xterm's
          colors dynamically, i.e., after it was started.  They are not
          the same as the ANSI colors.  These controls may be disabled
          using the allowColorOps resource.  At least one parameter is
          expected for Pt.  Each successive parameter changes the next
          color in the list.  The value of Ps tells the starting point
          in the list.  The colors are specified by name or RGB specifi-
          cation as per XParseColor.

...

            Ps = 1 0  -> Change VT100 text foreground color to Pt.
            Ps = 1 1  -> Change VT100 text background color to Pt.

With xterm (not necessarily for "xterm" imitators...),

printf '\033]11;blue\007'
printf '\033]11;white\007'

changes the window background to blue and then white.

Unlike settings for "ANSI colors", the dynamic colors persist, will not be reset by other escape sequences.

Oddly, xterm: how to change the background color? suggests "two" (actually one) different way for setting the background color, but that's not useful for your purpose.

Community
  • 1
  • 1
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105