Vim 256 colour strange behaviour with tmux

2.9k Views Asked by At

Just an interesting little problem I've got going on with terminals. I'm using vim inside of gnome-terminal. I like to have 256 colours so I don't have to use gVIM.

My relevant .zshrc settings are:

export TERM=xterm-256color

My relevant .vimrc settings are:

if $COLORTERM == 'gnome-terminal'
  set t_Co=256
endif

You'll notice from this showterm what the problem is: http://showterm.io/06460aeb043cac4bd0256

I'm getting a weird background colour with the tmux vim. Any help on this would be great.

2

There are 2 best solutions below

2
On

This is just a quick investigation.

  1. after execute tmux inside gnome-termnial, "COLORTERM" will be overwritten. That means $COLORTERM is NULL inside tmux.

The flow of these variables.

  1. gnome-terminal starts and load your .zshrc. (TERM=xterm-256color COLORTERM = gnome-terminal)
  2. tmux starts. (it overwrite COLORTERM, load its config "default-terminal" as the value of TERM)
  3. tmux load .zshrc (TERM is rewritten to "xterm-256color")
  4. Now => TERM = "xterm-256color" COLORTERM = ""
  5. vim starts => (besides your .vimrc config, t_Co is set to 256 if the term is 256 color)

The problem is that tmux support 256 color correctly only if TERM="screen-256color" instead of "xterm-256color".

Below is a solution:

First, add this to your .zshrc(I use bash syntax):

if [[ $COLORTERM == 'gnome-terminal" ]]; then
            export TERM="xterm-256color"
fi

Second, add this to your "~/.tmux.conf":

set -g default-terminal screen-256color

Last, vim settings is not needed.

Any further questions, please comment.

1
On

Try the following. This has worked for me:


.tmux.conf

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

Also, remove the old term value for .vimrc