Console Things - Getting 24-bit color working in terminals

Blog Post created by csawyer Employee on Dec 22, 2018

One of the things that's come back with a vengeance from the 1970s is the need for terminal displays. No, I'm not talking DEC-VT52s roaming about...




It's headless displays. Most server computers and nearly all IoT devices are being set up these days with no need for a full GUI. Not only does that free up more space for the processor and hands over an extra 100MB-1GB of RAM for your business tasks (depending on what operating system you're using), it also forces you to invest more into remote automation to administrate your work.


Think PowerShell on Windows or ssh+mosh with rsync and scp on Linux-based machines. (If you don't know what mosh is, you should stop right now and install it; then use mosh instead of ssh to connect. This is a godsend if you're on wireless or LTE connections)


One thing that hasn't caught up is the video


The default behavior and capabilities in most emulated terminals is fairly horrific. If you use color you're usually messing about with a bland CGA-style 16-color palette that's actually older than I am. If you're on Linux you might be using xterm-256 color for your defaults; if you remembered to set it and your terminal program didn't send the Linux box a terminal type that actually does date back to the 1970s, like vt100.


Things are a bit better now in Windows but only if you are on a recent edition.


Support for 24-bit color (millions of colors) inside terminals made its way into Linux around 2014 and in Windows 10 shortly after its first release. For Windows .NET programmers you can easily take advantage of this by extending System.Console. The Pastel package on GitHub under the MIT license makes quick work of this. As older versions of Windows eventually drop off you should see more console-based tools and applications sending more colorful output with ANSI codes.


Why 24-bit color?


"640k outta be good enough for everybody", right? To remove any doubt that we all still live inside terminals, there is a special form of YouTube called Asciinema that is built specifically for recording what you're doing inside a terminal so you can share it with others. Why would you want to be stuck in monochrome or with a nasty 16-color palette that's dated?


I will concede the benefits don't seem immediately obvious. It isn't until you are doing more of your DevOps, editing, debug and compile work in a terminal. Vim and Emacs both support 24-bit color and with that you can greatly reduce eye-strain with new themes that take advantage of this new color space...


The more headless server programs we write, the more flying-debug console output we tend to create. Color coding serious messages vs. inconsequential ones can rapidly draw your eye to a problem in real-time. It helps to use a pleasing color selection for that work. Combined with the right font to ease readability this can make your work far more pleasing and productive. My terminal font preference is Insonsolata-g which is free and considered one of the top-tier terminal fonts, or PragmataPro which I use to code and supports ligatures.


Set Linux up for 24-bit color


Getting 24-bit color enabled takes a bit more work. For one thing, the widely used distros out there do not ship with an xterm-24bit terminal capability setting that you can just quickly use.

So to get around that, let's create this file in our user directory and call it xterm-24bit.src by issuing the command `nano xterm-24-bit`.


# Use colon separators.

xterm-24bit|xterm with 24-bit direct color mode,




# Use semicolon separators.

xterm-24bits|xterm with 24-bit direct color mode,





Press Ctrl+X to exit the text editor and hit YES to save the file.


Now, let's install this terminal capability into our user account so it's there when we sign on next time:


$ tic -x -o ~/.terminfo xterm-24bit.src


After you do this, let's use this setting. You can do this one of two ways; you can go into your terminal emulator you are using and have it send the terminal type. Here's where that setting is in PuTTY:



Change this to xterm-24bit or xterm-24bits.


Another place you can set your terminal setting is in your user profile. This will be located in either ~/.profile, ~/.bashrc, or even ~/.bash_profile.


Just add this into your user profile script file: export TERM=xterm-24bit


I don't recommend that you do this for the entire system (i.e. editing /etc/profile) unless you're certain that everyone who would access the machine would be using the latest builds of terminal software. Also if you are using Linux or MacOS as your desktop and not Windows you will need to edit both your local computer's terminal capability as well as the remote host otherwise you may run into trouble with your terminal trying to interpret the color codes.


How to test that 24-bit color is working


A very fast way is to paste this into your terminal and run it:


awk 'BEGIN{

s="/\\/\\/\\/\\/\\"; s=s s s s s s s s;

for (colnum = 0; colnum<77; colnum++) {

r = 255-(colnum*255/76);

g = (colnum*510/76);

b = (colnum*255/76);

if (g>255) g = 510-g;

printf "\033[48;2;%d;%d;%dm", r,g,b;

printf "\033[38;2;%d;%d;%dm", 255-r,255-g,255-b;

printf "%s\033[0m", substr(s,colnum+1,1);


printf "\n";




You should see a continuous unbroken bar of color from red to orange and green to blue with no visible blocks where the color gradient changes. There's more scripts available here where you can do more experiments with color.


I have 24-bit color working in the terminal but then it doesn't work in tmux


If you use a terminal multiplexer like tmux you'll discover that 24-bit color doesn't work. At least not right out of the gate. You will need to manually flip another bit to get this to work. Edit or create a .tmux.conf file in your user folder and insert these lines:


$ nano ~/.tmux.conf


set -g default-terminal "xterm-24bit"
set -g terminal-overrides ',xterm-24bit:Tc'


Close down tmux if you have it running, then log out and log back in and fire tmux up again. Your 24-bit color should now be operational.


On some remote hosts I can't change to support 24-bit color and I get garbage on my screen when I connect to them now


Just set your local terminal back to its old setting right before you ssh like this:


$ TERM=xterm-256color ssh remotehost.mycompany.com


Or if you are on PuTTY, don't use the xterm-24bit terminal type for those hosts.