108

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?

2
  • What happens if you press control-l in normal mode, or execute the ":redraw" command?
    – Heptite
    Commented Mar 10, 2012 at 22:10
  • @Heptite nothing happens still the color change is per line like in the screenshot Commented Mar 10, 2012 at 22:19

8 Answers 8

81

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

8
  • 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 Commented Mar 11, 2012 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
    Commented Mar 11, 2012 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
    Commented Mar 11, 2012 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. Commented Nov 10, 2013 at 23:45
  • Try if [[ $TERM == xterm ]]; then TERM=xterm-256color; fi, it worked for me.
    – asymmetric
    Commented Apr 30, 2015 at 16:44
65

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.

4
  • 7
    this the only thing that finally worked for me!
    – oz123
    Commented Jun 7, 2013 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
    Commented Jan 17, 2015 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
    Commented Sep 19, 2015 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.) Commented Nov 29, 2016 at 21:39
17

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.

5

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
1
  • No. Don't. I have never seen a case where you need to manually set your $TERM. The only correct way is tmux configuration. Both Mac and any Linux. Commented Nov 22, 2019 at 22:54
2

If you still face issues: I noticed that vim falls back to using option t_Co=8 inside tmux even if $TERM is set to screen-256color. My workaround is this snippet in vimrc:

if exists("$TMUX")
        set t_Co=256
        set notermguicolors
else
        set termguicolors
endif

The $TMUX variable is only filled if inside a tmux session. In this case, I allow vim to use 256 colors. Note that I also unset termguicolors as tmux does not support true colors.

1

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.

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

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.

0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .