2

If I want the ANSI color 0 to be red, in the urxvt terminal, I need to pass the sequence \e]4;0;red\a to the latter:

printf '\e]4;0;red\a'

I found the general syntax here:

OSC 4 ; c ; spec BEL

Inside tmux, it doesn't work, maybe because it's consumed by tmux before the terminal. So, I need to protect it via another sequence found here:

printf '\ePtmux;\e\e]4;0;red\a\e\\'

Now if I want to apply a style to the text, like underlining it for example, whether I'm inside or outside tmux doesn't matter. The same sequence seems to always work:

printf '\e[4m underline \e[0m'

I thought that maybe this difference could be explained because I've set up some options in ~/.tmux.conf. In particular, I set the option terminal-overrides to add and set the unofficial terminfo extensions Ss and Se to change the shape of the cursor inside tmux as explained in man tmux (section TERMINFO EXTENSIONS):

set-option -as terminal-overrides ',*:Ss=\E[%p1%d q:Se=\E[2 q'

But when I tried to apply a style to the text inside tmux, I started the latter without any configuration:

tmux -Ltest -f/dev/null

Inside tmux, why don't you need to protect the sequence \e[4m underline \e[0m like you need to for \e]4;0;red\a?

  • 1
    \e]4;0;red\a is not the right way of changing the text's color to red (actually I'm wondering why it works for you at all). This sequence redefines the actual color of the palette index 0 (which is usually black) to be red from now on. The proper way to switch to red is to switch to red (\e[31m) and not to redefine black globally to be red from now on (even affecting previously printed text). – egmont Feb 12 '18 at 21:27
  • @egmont You're totally right. I'm using zsh and the zsh-syntax-plugin, and so, on my machine, changing the color of index 0 has the effect of altering the default color of the text on the command-line (the one used for the text which is not recognized as anything special: command name, string, ...). Sorry for the confusion. I'm going to try and fix the original post. – user786441 Feb 12 '18 at 21:46
5

tmux is not XTerm (even if you are using it inside XTerm). It acts as its own terminal emulator (and, of course, multiplexer) on top of whatever terminal you happen to be using. The page you linked is XTerm control sequences, which (while very useful) is not applicable to every terminal in existence. For screen, the online manual page screen(1) lists the control sequences it accepts. tmux(1) does not contain a similar section, but there is an old description of various control sequences for terminals dated 1984, reflecting at least what its authors aimed for several years ago albeit not strictly documenting what its current behaviour is, in its source code in tools/ansicode.txt.

In any case, the SGR sequences for setting text attributes are more universally supported than the "Operating System Commands", such as the one you use to change the color palette. From the same linked page:

CSI Pm m Character Attributes (SGR)

  • Ps = 0 -> Normal (default).
  • Ps = 1 -> Bold.
  • Ps = 2 -> Faint, decreased intensity (ISO 6429).
  • Ps = 3 -> Italicized (ISO 6429).
  • Ps = 4 -> Underlined
  • Ps = 5 -> Blink (appears as Bold in X11R6 xterm).
  • Ps = 7 -> Inverse.
  • Ps = 8 -> Invisible, i.e., hidden (VT300).
  • Ps = 9 -> Crossed-out characters (ISO 6429).
  • Ps = 2 1 -> Doubly-underlined (ISO 6429).
  • Ps = 2 2 -> Normal (neither bold nor faint).
  • Ps = 2 3 -> Not italicized (ISO 6429).
  • Ps = 2 4 -> Not underlined.
  • Ps = 2 5 -> Steady (not blinking).
  • Ps = 2 7 -> Positive (not inverse).
  • Ps = 2 8 -> Visible, i.e., not hidden (VT300).
  • Ps = 2 9 -> Not crossed-out (ISO 6429).
  • Ps = 3 0 -> Set foreground color to Black.
  • Ps = 3 1 -> Set foreground color to Red.
  • Ps = 3 2 -> Set foreground color to Green.
  • Ps = 3 3 -> Set foreground color to Yellow.
  • Ps = 3 4 -> Set foreground color to Blue.
  • Ps = 3 5 -> Set foreground color to Magenta.
  • Ps = 3 6 -> Set foreground color to Cyan.
  • Ps = 3 7 -> Set foreground color to White.
  • Ps = 3 9 -> Set foreground color to default (original).
  • Ps = 4 0 -> Set background color to Black.
  • Ps = 4 1 -> Set background color to Red.
  • Ps = 4 2 -> Set background color to Green.
  • Ps = 4 3 -> Set background color to Yellow.
  • Ps = 4 4 -> Set background color to Blue.
  • Ps = 4 5 -> Set background color to Magenta.
  • Ps = 4 6 -> Set background color to Cyan.
  • Ps = 4 7 -> Set background color to White.
  • Ps = 4 9 -> Set background color to default (original).

(I have a feeling Pm was meant to be Ps to match the items.)

It makes sense that tmux would support these directly, as they are often used by applications, and users might be frustrated by lack of support.

It might also be worth noting that the Linux console uses a different escape sequence to set palette index 0 to red: \033]P0ff0000\033\\. In general it is OSC P n rr gg bb ST where n is the palette index (in hex) and rr gg bb is the color (also in hex).

  • 1
    Unicode rxvt, which is what the questioner is using, is not XTerm either. (-: Also note that technically what the questioner was doing is changing the palette entry for black (#0) to red, not changing the foreground colour to red (palette entry #1). So SGR is not a direct equivalent to what the questioner was doing, even though it is possibly what the questioner really wants to do. – JdeBP Feb 12 '18 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.