Tmux and 256 colors

I am unable to get tmux to work with 256 colors and I am at a complete loss as to why it doesn’t work. To this end I have constructed a minimal reproducer that can be used with nixos-shell

{ config, lib, pkgs, ... }: {
  # just so that I don't have to download another kernel
  boot.kernelPackages = pkgs.linuxPackages_latest;

  virtualisation.graphics = true;
  nixos-shell.mounts = {
    mountHome = false;
    mountNixProfile = false;
  };

  services.xserver = {
    enable = true;
    desktopManager.xterm.enable = true;
    displayManager.startx.enable = true;
  };

  programs.tmux = {
    enable = true;
    terminal = "tmux-256color";
    extraConfig = ''
      set-option -ga terminal-overrides ",*256col*:Tc:RGB"
    '';
  };

  environment.systemPackages = [
    (pkgs.neovim.override {
      configure.customRC = ''
        set termguicolors
      '';
    })
  ];
}

After starting up the VM, log in as root (no password) and run startx to start the X server. A few xterm windows should pop up. Now you can run nvim /etc/bashrc with and without tmux and compare the output. Here is a screenshot, left is inside tmux, right is outside.

How do I fix the colors in tmux?

2 Likes

Here’s my home manager config for tmux.

One thing that is tricky about tmux configuration is that it won’t reload its configuration if a session is still running, so make sure you killall tmux between changes.

There are no changes being made here. All the config files are already there when the VM starts up, i.e. there is no running session. Yet it doesn’t work.

What is TERM before and after starting tmux?

On my machine TERM=xterm-256color.


Hm, I realized that my minimal reproducer doesn’t work correctly. It sets TERM=xterm and for some reason tmux doesn’t read its own config.

1 Like

Okay, now I’ve cleared out all my config files, so I have

$ xrdb -query
xterm*termName: xterm-256color
$ echo $TERM
xterm-256color
$ cat .tmux.conf
set-option -g default-terminal "tmux-256color"
set-option -ga terminal-overrides ",xterm-256color:Tc"
$ cat .config/nvim/init.vim
set termguicolors

The broken behavior persists (left with tmux, right without):

Are those commands being run inside or outside of tmux? I’d like to see

echo $TERM

inside and outside of tmux.

Outside

$ echo $TERM
xterm-256color

Inside

$ echo $TERM
tmux-256color

Let me try to resurrect this old thread.

To make sure that this is not an effect my problematic setup, I’ve downloaded the latest NixOS 21.05 Live CD (iso download link) and booted it in QEMU:

qemu-system-x86_64 -enable-kvm -cpu host -smp 4 -m 2048 -drive file=nixos-gnome-21.05.1510.a165aeceda9-x86_64-linux.iso,media=cdrom

Then I installed neovim and tmux using

nix-env -iA nixos.neovim
nix-env -iA nixos.tmux

The .tmux.conf contains

set-option -g default-terminal "screen-256color"
set-option -ga terminal-overrides ",*256col*:Tc"

The .vimrc contains

syntax on
set termguicolors

And here is the screenshot:

The colors are clearly not the same and the left (inside tmux) is pretty unreadable. I don’t understand what is going on and why this is happening. Please help!

I just setup the same VM as you did, except my config was a bit leaner than yours:

# .tmux.conf
set-option -g default-terminal "screen-256color"
# .vimrc
syntax on

I checked the colors without vim with this bashscript[via] and the output seems to be ok in both cases (tmux and no tmux).

I checked the colors within vim with :runtime syntax/colortest.vim and they seem to work correctly in both cases.

I checked with different terminal emulators: konsole, lxterminal, lxqt.qterminal they render the color in the same way whether vim is started within tmux or not, but differ between terminals.

If you add you favorite vim colorscheme to your .vimrc the output is identical in both cases: nested in tmux or not

:colorscheme desert

so it seems like it’s a special configuration adaption for the gnome terminal. still strange why nvim chooses a different default colorscheme whether it’s started in tmux or not…

digging though the neovim issues it seems like the background-color is not detected correctly: if you issue :se bg the nvim nested in tmux outputs background=dark, nvim started directly in gnome terminal outputs background=light. via #14058

1 Like

The background color thing looks like the solution. After setting

set termguicolors
set background=light

in .config/nvim/init.vim and

(setq frame-background-mode 'light)

in .emacs.d/init.el the colors are such that everything is readable again everywhere.

Thank you very much!

1 Like