Fixing tmux for 256 colors

October 20, 2024

Background

Recently, I’ve been experimenting with WezTerm and Neovim. And I’ve also been trying various colorschemes. 1 Things didn’t look right, but since I had been changing so many things at the same time, it was hard to pinpoint the cause.

It’s subtle, but it’s there. Neovim running outside tmux:

nvim running outside tmux

Neovim running inside tmux:

nvim running inside tmux

(This is an example of what makes it hard for people to switch their core tools. After spending so much time in (classic) Vim, I’ve managed to track down all these nits …)

Tracking it down

Thanks to Setting up True Color Support for Neovim and Tmux on Mac, try and give this AWK script a go (in bash or zsh)

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";
}'

If everything is configured properly, it looks like the first one

a comparison between a uniform and jagged band of colors

if NOT, it’s closer to the second one. (in tmux, misconfigured)

Recommendations

Ensure your TERM variable, outside tmux, says xterm-256color

> echo $TERM
xterm-256color

# in practice, in your shell startup script
export TERM="xterm-256color"
# or fish equivalent ...

but the details depend on what terminal you’re running. The last piece is to put

set -a terminal-features 'xterm-256color:RGB'

in your tmux.conf file. You will have to restart tmux for this change to take effect.

Trust Neovim

One thing that put me on the right track was running :checkhealth in Neovim
(line 42)

neovim's checkhealth gives a tmux recommendation

You might want to check your output (EVEN if you don’t intend to running Neovim) and see what other tidbits are in there.

Once I saw that, I knew exactly what to look for in Google and StackOverflow.


  1. interesting stories in themselves … for another day 

Discuss on Twitter