Why do Vim colors look different inside and outside of tmux

colorsterminaltmuxvim

Environment:

  • Fedora 25 (4.9.12-200.fc25.x86_64)
  • GNOME Terminal 3.22.1 Using VTE version 0.46.1 +GNUTLS
  • VIM – Vi IMproved 8.0 (2016 Sep 12, compiled Feb 22 2017 16:26:11)
  • tmux 2.2

I recently started using tmux and have observed that the colors within Vim change depending on whether I'm running inside or outside of tmux. Below are screenshots of Vim outside (left) and inside (right) of tmux while viewing a Git diff:

enter image description here

My TERM variable is

  • Outside tmux: xterm-256color
  • Inside tmux: screen-256color

Vim reports these terminal types as expected (via :set term?):

  • Outside tmux: term=xterm-256color
  • Inside tmux: term=screen-256color

Vim also reports both instances are running in 256-color mode (via :set t_Co?):

  • Outside tmux: t_Co=256
  • Inside tmux: t_Co=256

There are many similar questions out there regarding getting Vim to run in 256-color mode inside tmux (the best answer I found is here), but I don't think that's my problem given the above information.

I can duplicate the problem outside of tmux if I run Vim with the terminal type set to screen-256color:

$ TERM=screen-256color vim

So that makes me believe there's simply some difference between the xterm-256color and screen-256color terminal capabilities that causes the difference in color. Which leads to the question posed in the title: what specifically in the terminal capabilities causes the Vim colors to be different? I see the differences between running :set termcap inside and outside of tmux, but I'm curious as to which variables actually cause the difference in behavior.

Independent of the previous question, is it possible to have the Vim colors be consistent when running inside or outside of tmux? Some things I've tried include:

  • Explicitly setting the default terminal tmux uses in ~/.tmux.conf to various values (some against the advice of the tmux FAQ):
    set -g default-terminal "screen-256color"
    set -g default-terminal "xterm-256color"
    set -g default-terminal "screen.xterm-256color"
    set -g default-terminal "tmux-256color"
  • Starting tmux using tmux -2.

In all cases, Vim continued to display different colors inside of tmux.

Best Answer

tmux doesn't support the terminfo capability bce (back color erase), which vim checks for, to decide whether to use its "default color" scheme.

That characteristic of tmux has been mentioned a few times -

Related Question