tmux, TERM and 256 colours support

68,378

Solution 1

There is some information on 256-color support in the tmux FAQ.

Detecting the number of colors that the terminal supports is unfortunately not straightforward, for historical reasons. See Checking how many colors my terminal emulator supports for an explanation. This means that

  • tmux cannot reliably determine whether the terminal supports more than 8 colors;
  • tmux cannot reliably communicate to the application that it supports more than 8 colors.

When you're in tmux, the terminal you're interacting with is tmux. It doesn't support all of xterm's control sequences. In particular, it doesn't support the OSC 4 ; … control sequence to query or set color values. You need to use that while directly running in xterm, outside tmux.

If you run tmux -2, then tmux starts with 256-color support, even if it doesn't think that your terminal supports 256 colors (which is pretty common).

By default, tmux advertises itself as screen without 256-color support. You can change the value of TERM in .tmux.conf to indicate 256-color support:

set -g default-terminal "screen-256color"

You can use TERM=xterm-256color or TERM=screen-256color on Ubuntu. These values will only cause trouble if you log in to a remote machine that doesn't have a termcap/terminfo entry for these names. You can copy the entries to your home directory on the remote machine; this works with most modern terminfo implementations.

# From the Ubuntu machine to a machine that doesn't have *-256color terminfo entries
ssh somewhere.example.com mkdir -p .terminfo/s .terminfo/x
scp -p /lib/terminfo/s/screen-256color somewhere.example.com:.terminfo/s/
scp -p /lib/terminfo/x/xterm-256color somewhere.example.com:.terminfo/x/

Solution 2

I'm impressed about how richfully formatted and detailed the answers (and the question!) are. While they provide valuable info and solutions about the tools you mentioned, they provide very little insight about what the heck is going on, and, most importantly, why things are (somewhat) working for some tools when they supposedly should not.

So here's some insights on your fundamental questions:

  • What a terminal supports and what it reports are different things. Gnome terminal for example is capable of displaying 256 colors, but it announces itself to the environment (via $TERM variable) as an xterm emulator (8 color).

  • Tools like tput follow whatever TERM is set to: tput colors may print 8, while env TERM=xterm-256color tput colors will print 256, regardless if your terminal actually supports such capabilities.

  • vim also follow TERM by default, but as you told it to use 256 colors (via the flag or the set t_Co=256), it will use 256 colors. And it works because your terminal actually supports that.

  • tmux, just like Gnome Terminal, also by default reports itself as an 8-color terminal. But, unlike Gnome Terminal, it only enables 256-color capability if you use the -2 flag, which also makes it report itself as an xterm-256color compatible emulator.

  • xterm, as in the terminal emulator software for the X11, supports 256 colors, for sure. But xterm as in TERM=xterm is an 8-color "standard". It means the capabilities of the original xterm. When it was upgraded to support 256 colors, a long time ago, it coined the xterm-256color term for that.

So it all comes down to:

  • What capabilities your terminal actually supports (and is enabled to do so)

  • How it reports such capabilities to the environment via TERM

  • How tools interpret TERM and adjust themselves accordingly.

  • As you can infer from the above, avoid hammering export TERM=xterm-256color in your ~/.bashrc and such scripts. As those files are executed regardless of the terminal you're actually using, it will make all your terminals, including remote connections from Windows with Putty, Linux console terminal, etc, report themselves as xterm-256color compatible. Which may be true for some but certainly not for all. For example, getty, the linux console you access with CTRL+ALT+1..6, does not.

  • Terminals should report themselves as the "standard" they are most compatible with. If you know they can handle 256 colors, configure them to advertise as such.

Last but not the least, an amazing reading about TERM and 256 colors is:

http://blog.sanctum.geek.nz/term-strings/

http://blog.sanctum.geek.nz/256-colour-terminals/

Solution 3

I will provide a complementary answer which addresses only your additional experiments insofar as they relate to the solarized directory colors.

Setup

We have the same color output from the test script. The only difference is that I used lxterminal on Openbox with slim, xcompmgr and no DM. Therefore I can't easily set a custom palette like you did as that terminal emulator doesn't offer those settings in a GUI and there is no specific theme for it (those only exist for a few terminal emulators). So I just use some translucency, undecorated terminals and that blue as foreground color with a blue Gentoo background. Since I can't use the ansi versions of the color files, I've focused exclusively on getting results with dircolors.256dark i.e the "degraded" simple version.

I used the following relevant configuration and the inconsolata medium font:

echo $TERM
xterm

.bashrc:
eval `dircolors /path/to/dircolors.256dark`

.vimrc:
set t_Co=256
let g:solarized_termcolors=256
syntax enable
set background=dark
colorscheme solarized

.tmux.conf:
nothing at all related to colors!
start tmux with "tmux -2"

Dircolors

The image below shows what happens both inside and outside of tmux with these settings. The first third to the left shows undecorated lxterminal windows stacked vertically (3). To the right you have a tmux assembly showing the same identical programs(3) I have also included a solarized xterm1 showing nano using the .Xresources file included in the full package (and sampling it with xrdb -load ~/.Xresources):

enter image description here please right click/view image to inspect at full resolution

The first terminal in the top left corner showcases the default directory colors. Just below is the degraded solarized version. Except for the pink on blue background for .c files I added, it is identical to what is expected from this(see image below for reference). Compared to the defaults, it builds further on the extended attributes i.e. bold/light/reverse etc. and the colors are different obviously. The default color assignment for .txt files in many distribution is green, but it should be grey instead when solarized. An ansi file rendering a .txt file as green is either not rendering correctly or not rendering at all. The right hand-side results you showed are the correct ones(256dark) in relation to the following reference:

enter image description here solarized "degraded" dircolors reference map

Observations

With the configuration I used, the results appear identical inside and outside tmux(I have reversed comments (#) in vi but otherwise the plugin behaves as it should and the multiplexer has no impact on this). Fonts play a huge role in the solarized features definition, and a good font is required to maximize the experience. The solarized directory colors using the 256dark file match the reference and requires no specific terminal emulator configuration.


Conclusion

Actually the ansi rendering of the directory colors is just completely different from the degraded solarized(256dark). So much so that under ansi .txt files are green. One cannot be used to validate the rendering of the other. Both solutions need different configuration and produce entirely different results.

Share:
68,378

Related videos on Youtube

Atcold
Author by

Atcold

Musician, math lover, cook, dancer, 🏳️‍🌈, and an ass prof of Computer Science at New York University

Updated on September 18, 2022

Comments

  • Atcold
    Atcold over 1 year

    Introduction

    My question arises from the necessity of understanding why I have now (after multiple trials) Terminal and tmux supporting 256 colours and tput colors telling me there are only 8 of them.


    Background

    Let's start from the beginning.

    I'm using a Ubuntu box, Guake, tmux, Vim and I love the theme Solarized. They were looking pretty awful, so I decided to enable the 256 colours support and play a bit around.

    Let's see what happens for my Terminal. tput colors says there are 8 colours. I personally set them to purple, on the left, and of course on the right we have 2 different shades of blue. $TERM says xterm. (To have the coloured ls I eval this in my .bashrc.)

    enter image description here

    Vim also looks fine, despite the fact that I call it with the 256 flag in an environment where 256 colours are not supported.

    set t_Co=256
    let g:solarized_termcolors=256
    colorscheme solarized
    

    The only guy who complains about the reduced colour space is tmux. Calling tmux provides the "wrong" expected results.

    enter image description here

    But calling tmux with the -2 flag makes everything work fine, magically.

    enter image description here

    Now the only thing that I understand is that -2 is equivalent of export TERM=screen-256color (source).

    Guake behaves analogously to Terminal and both of them answer xterm to the question echo $TERM.


    Question

    Basically, does anyone understand why everything works even if it shouldn't?

    • Am I sadistic that I complaining why things work? Maybe.
    • Is there a better reason? Sure: I'd like to fix the appearance of other Ubuntu boxes in my office, and I'd like to understand why things work or don't work.

    Additional experiments

    Running this script (slightly modified) on my xterm provides the following result: 256 colours, but only 16 are displayed correctly.

    enter image description here

    Then, changing terminal's profile, also these 16 colours change.

    enter image description here

    More tests are following.

    From left to right, top to bottom, we have Solarized colour theme, dircolor ansi-dark and 256dark, then default (Tango) colour scheme, dircolor ansi-dark and 256dark.

    enter image description here

    Observation: in theory the dircolor ansi-dark on Solarized colour scheme should have match closely the dircolor 256dark. This is not clearly happening for the specific listed files. Instead, this quite happens when in the working directory there are folders, text files and symbolic links. Conclusion: no much attention as been paid while encoding the 256dark colours.


    Preliminary conclusions

    xterm supports 256 colours, despite what tput colors says. Programs can refer to the ansi palette (customisable by the user) or define their colours, picking from a total of 256 colours.

    • terdon
      terdon about 10 years
      Read this answer and the comments below it. Does it answer your question? Also have a look at this script for getting the real number of colors supported.
    • Atcold
      Atcold about 10 years
      Hum... I'm still slightly confused... But, I guess I'm at least understanding that tput colors is an unreliable test. Could you check my preliminary conclusions?
    • terdon
      terdon about 10 years
      My reading of Gilleś's answer is that tput colors can only return one value and in terminals that support any of 2,8,16,88 or 256 colors, only the first value (8 in your case) is returned. To get the true value use the script from my last comment. What does that return?
    • Atcold
      Atcold about 10 years
      Check my Additional experiments above :)
    • terdon
      terdon about 10 years
      Ah, sorry, my bad.
    • Atcold
      Atcold about 10 years
      Then, if I try to run the script from tmux it exits with 0 colours :( So, I'm not sure what's happening there...
  • Atcold
    Atcold about 10 years
    I do appreciate the explanation, but I'm not sure it targets my question.. Well, I agree that my question was quite broad, but I wasn't focusing specifically on tmux. Actually, tmux was the only guy who was behaving accordingly to expectation. What it was much more mysterious was how xterm was able to show me 256 colours even though tput colors was saying 8. I guess the answer to this specific question has been addressed by @terdon in his comments to the question itself.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 10 years
    @Atcold It's a long question, but as far as I can tell I've answered it. The non-tmux-related parts are covered by the answer that terdon and I linked to (and that I wrote). Your question would have been a duplicate of that one if it hadn't been for the tmux aspect. What more were you expecting from an answer?
  • Atcold
    Atcold about 10 years
    @Gilles, I didn't want to sound mean. I was trying to accredit to @terdon the "accepted answer" thing, even though he refers to your answer in another question. Despite this, the set -g default_terminal "screen-256color" command is not sufficient to enable the 256 colours in tmux. You need to alias tmux='export TERM=screen-256color; /usr/bin/tmux' (reference).
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 10 years
    @Atcold No, the alias isn't needed (I tested). Furthermore this alias wouldn't make sense: it pretends to tmux that it's running inside screen or tmux. However, you do need to run tmux -2 as per my previous paragraph, if tmux doesn't think your current terminal supports 256 colors, so alias tmux='tmux -2' would make sense.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 10 years
    @Atcold That U&L answer has TERM=xterm-256color tmux, which is sensible, unlike TERM=screen-256color tmux. tmux -2 is superior because it works even if screen-256color isn't in the local terminfo database.
  • Atcold
    Atcold about 10 years
    @Gilles, tmux -2 is not superior. Check my findings here, second Q/A.
  • Atcold
    Atcold about 10 years
    "I don't understand your conclusion about not much attention having been paid to the 256dark encoding where in fact it's the only color file which yields a solarized directory listing?" My confusion arises from the fact that I was considering the ansi-dark as correct output (since it's using the non degraded version). Then, if you take as reference the output of the 256dark, then you have the reversed mindset.
  • Atcold
    Atcold about 10 years
    "With the configuration I used, the results appear identical inside and outside tmux" Sure, if you're using 256 colours, then of course, they will render the same way wherever you have 256 colours support. "It's not clear what the ansi solution would further provide in this context..." The outcome would be having the true Solarized them (instead of an approximation).
  • zed
    zed about 10 years
    I understand. I haven't researched the ansi part really so I could not grasp where you were coming from. In any case I found your Q quite enabling thank you. Maybe I'll use vi more now that I find it pretty lol. As for the ansi results, actually now I've seen how it looks... they only showcase that for dir-colors here. Why the h. do they have different coding under that... I mean I see .txt green... how is grey close to green? I don't understand their project seriously.
  • Atcold
    Atcold about 10 years
    Yeah, I contacted the author personally and the answer is that "it was his personal customisation, since he liked that way". I'm speechless... I'm glad my Q helped someone :)
  • Sedrik
    Sedrik almost 10 years
    For me I had to change the command to set -g default-terminal "screen-256color" (- and not _ in default terminal)
  • Elijah Lynn
    Elijah Lynn almost 6 years
    An example scenario or two would be good additions to this answer.
  • Nick Dong
    Nick Dong over 2 years
    set -g default-terminal "tmux-256color" in ~/.tmux.conf works for tmux 2.7 and fedora 29 without other settings. see ref