256 color support for vim background in tmux

Solution 1:

From the look of your .bashrc and .profile, the shells inside tmux are overriding the 'default-terminal' setting in your tmux conf. Something like this:

  • tmux creates new shell with TERM=screen-256color
  • .bashrc/.profile run, set TERM=xterm-256color
  • vim runs, tries to use incorrect TERM for tmux

you can check this by running

echo $TERM

in a fresh tmux shell.

Tmux is relatively picky about having a terminal set correctly. If you can, set the term value in gnome-terminal's configuration, not in your .bashrc. Failing that, surround those settings with a check for "screen" or "screen-256color" TERM, and don't reset them in that case.

Tmux REALLY wants the terminal set to screen or screen-256color

Solution 2:

As explained here, disable Background Color Erase (BCE) by clearing the t_ut terminal option (run :set t_ut= in Vim and then press Control+L to refresh the terminal's display) so that color schemes work properly when Vim is used inside tmux and GNU screen.

Solution 3:

I've found a better way on this post. You can make an alias of tmux to tmux -2 which will force tmux to assume that the shell is using 256 color terminal.

Solution 4:

This is what worked for me in #Ubuntu and #Mac:

# File: ~/.bashrc (Ubuntu), ~/.bash_profile (Mac)
# for VIM and TMUC
if [ "$TERM" = "xterm" ]; then
  export TERM=xterm-256color
fi
alias tmux='tmux -2'  # for 256color
alias tmux='tmux -u'  # to get rid of unicode rendering problem

Reload settings:

$ source ~/.bashrc # Ubuntu

$ source ~/.bash_profile # Mac

Set up .bashrc for Mac (as it is used by tmux)

# File: ~/.bashrc (Mac)
source ~/.bash_profile

Set up "default-terminal" option in ~/.tmux.conf.

# File: ~/.tmux.conf
set -g default-terminal "screen-256color"  # Mac and Ubuntu