On the surface it seems like a simple question: how many colors are supported by a terminal you are using? This is important when configuring or debugging color problems in terminal programs like Vim.
The answer should be simple: just query the terminfo database. The easiest way to query the terminfo database to find this out is:
$ tput colors
The problem? The answer reported by this program is wrong! Under a GNOME Terminal which I can see rendering 256 colors, this reports 8 as the answer!
Why is it reporting the wrong answer? It seems to be misled by the
TERM environment variable. For reasons which only the Ubuntu/GNOME Gods can answer, the
TERM for GNOME Terminal is set to
xterm instead of
xterm-256color. If you set this manually to
tput reports 256 correctly. But then what is the point of that?
Another solution is to run the
terminalcolors.py Python script from here. This beautifully renders the colors in the shell. However, it also falls for the
TERM. Only if it is set to
xterm-256color can I see 256 colors rendered. Else only the first 8 colors are shown and the rest are in grey.
A similar output and behavior is seen by running
The final solution that worked for me was xterm-color-count. This Bash script actually tries all the XTerm Control Sequences and examines their responses to report the colors. This script was the only one which reported back 256 correctly for me for
Tried with: GNOME Terminal 3.6.2 and Ubuntu 14.04