while using vim within tmux I can see that 256 color support is enabled. with $tput colors

However changing the colorscheme in vim while in tmux will change the colorscheme on a per line basis but not the entire background. see screenshot enter image description here

Here is a snippet of the my .vimrc file for example. My original colorscheme is solarized dark and then after running :colorscheme molokai you see what happens.

info

  • gnome-terminal
  • bash

in my ~/.tmux.conf

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

in my ~/.vimrc

    set t_Co=256

in my ~/.bashrc

# ryan
export TERM="xterm-256color"
# ryan
alias tmux="tmux -2"

in my ~/.profile

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

Any ideas how I can get a full colorscheme change in vim? Are all my snippets from the files looking good?

  • What happens if you press control-l in normal mode, or execute the ":redraw" command? – Heptite Mar 10 '12 at 22:10
  • @Heptite nothing happens still the color change is per line like in the screenshot – winchendonsprings Mar 10 '12 at 22:19
up vote 73 down vote accepted

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

  • 5
    Well you fixed it. I simply commented out anything in my .bashrc and my .profile files that had to do with setting the color to 256. Restarted tmux and vim works with the proper background and 256 colors enabled. Can you tell me where the gnome-terminal config file is? Also when you say tmux wants terminal set to 'scfeen-256color, Do you mean like I have it in my tmux.conf? Thanks – winchendonsprings Mar 11 '12 at 5:00
  • I don't have a machine with gnome-terminal on it handy to check, but the setting should be somewhere in the GUI, probably under 'profile settings' or something like that. – bloy Mar 11 '12 at 15:19
  • 5
    Also, yes, let tmux do its own thing to the shells it spawns. Leave the set -g default-terminal "screen-256color" line in your tmux conf. – bloy Mar 11 '12 at 15:21
  • 2
    All of the above is what I was after (making sure TERM correctly set and not overridden in tmux), and THEN I had to use 'tmux -2' to load tmux up. – phantomwhale Nov 10 '13 at 23:45
  • Try if [[ $TERM == xterm ]]; then TERM=xterm-256color; fi, it worked for me. – asymmetric Apr 30 '15 at 16:44

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.

  • 7
    this the only thing that finally worked for me! – Oz123 Jun 7 '13 at 8:07
  • 3
    I ended up adding set t_ut= to my vimrc which removes the need to manually use the command and there is no need to use <kbd>Control</kbd> + <kbd>L</kbd> since the session starts with BCE disabled. – erran Jan 17 '15 at 19:56
  • This is essential when using PuTTY. All the TERM combinations fall short when the colorscheme has a background. Great addition to the thread. – botimer Sep 19 '15 at 16:18
  • This works, however when this solution is applied copying and pasting with the mouse also copies and pastes the spaces trailing lines (if they are highlighted.) – horse hair Nov 29 '16 at 21:39

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.

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

A quick fix is to run the following in the terminal.

export TERM=xterm-color

You could add it to your ~/.bash_profile or other profile to always be set on start.

  • Thanks! This was all I needed to add to my .zshrc to get visual mode to actually be visible. – danmcardle May 5 '16 at 14:30

I had very similar problem for gnome-terminal + tmux + vim but it was extended also to specific key combination problem: . I had to combine a few things together. first of all I had to setup my .tmux.conf into:

# Ensure terminal starts with its own colour scheme (helps Vim/Neovim themes to not break)
set -g default-terminal "xterm-256color"
# set -g default-terminal "screen-256color" # no S-Fx keys ;-(
# and ensure the key-codes are xterm alike
set -g xterm-keys on

Then in .vimrc:

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

And that did the job for the keys and background.

Just now faced the same problem. Based on bloy's answer and current content of my ~/.profile (actually, it's the same as winchendonsprings') I've solved my problem as follows:

~/.profile:

if [[ -z $TMUX ]]; then
    if [ -e /usr/share/terminfo/x/xterm+256color ]; then # may be xterm-256 depending on your distro
        export TERM='xterm-256color'
    else
        export TERM='xterm'
    fi
else
    if [ -e /usr/share/terminfo/s/screen-256color ]; then
        export TERM='screen-256color'
    else
        export TERM='screen'
    fi
fi

Logic is simple: if we're not inside tmux session, then use xterm (colored or not). Otherwise the same thing for screen.

Maybe it's too verbose, but you got the idea.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.