Background color mismatch in vim and tmux

colorsitermterminaltmuxvim

I'm having trouble getting the full range of colors to work in vim when I'm running through tmux. I belive that it's some sort of trouble with TERM variables or 256colors, but I've done everything I have been able to find online to get 256 colors working in vim, tmux, and iTerm, and nothing has fixed it. It's a small problem, but it seriously bugs me.

Here's an example code file in vim just through iTerm:
iTerm

and here's the same file in vim through tmux and iTerm:
enter image description here

Notice how the background colors seem slightly mismatched, only when code is written there. Why could this be?

I have set t_Co=256 in my vimrc, my iTerm terminal is set to xterm-256color, I have set -g default-terminal xterm-256color in my tmux.conf, and I have:

if [ -e /usr/share/terminfo/x/xterm-256color ]; then
     export TERM='xterm-256color'
else
     export TERM='xterm-color'
fi

in my .profile. This exact issue is replicated on my Ubuntu based machine at work, and I use all of the same configuration files there. This at least isolates the issue as not being OS/iTerm related.

Best Answer

After adding the line below into .tmux.conf

set -g default-terminal "screen-256color"

You still need to add the line below into .vimrc

set term=screen-256color

Finally, the alias need to be added to .bashrc

alias tmux='tmux -2'
Related Question