256 color support for vim background in tmux

99

55

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?

winchendonsprings

Posted 2012-03-10T21:56:21.027

Reputation: 1 848

What happens if you press control-l in normal mode, or execute the ":redraw" command? – Heptite – 2012-03-10T22:10:44.420

@Heptite nothing happens still the color change is per line like in the screenshot – winchendonsprings – 2012-03-10T22:19:18.093

Answers

76

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

bloy

Posted 2012-03-10T21:56:21.027

Reputation: 876

Try if [[ $TERM == xterm ]]; then TERM=xterm-256color; fi, it worked for me. – asymmetric – 2015-04-30T16:44:57.743

echo $TERM did it for me. Found out I had a line in my ~/.profile (which I put there myself and forgot about it...) that messed things up. Thanks. – Erwin Rooijakkers – 2015-12-27T11:47:47.590

@asymmetric No, don't mess with $TERM in shell configuration. It bites you back later because it's an anti pattern. Do yourself a flavor and set it in tmux configuration or terminal configuration. – Franklin Yu – 2019-11-22T22:57:03.343

5Well 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 – 2012-03-11T05:00:43.567

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 – 2012-03-11T15:19:42.910

5Also, 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 – 2012-03-11T15:21:42.410

2All 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 – 2013-11-10T23:45:10.550

60

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.

sunaku

Posted 2012-03-10T21:56:21.027

Reputation: 1 086

3I 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 – 2015-01-17T19:56:01.940

This is essential when using PuTTY. All the TERM combinations fall short when the colorscheme has a background. Great addition to the thread. – botimer – 2015-09-19T16:18:06.147

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 – 2016-11-29T21:39:41.263

7this the only thing that finally worked for me! – Oz123 – 2013-06-07T08:07:15.253

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.

Boogy

Posted 2012-03-10T21:56:21.027

Reputation: 546

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

Munish

Posted 2012-03-10T21:56:21.027

Reputation: 141

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. – Franklin Yu – 2019-11-22T22:54:20.063

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.

P4C

Posted 2012-03-10T21:56:21.027

Reputation: 111

1

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.

lgalke

Posted 2012-03-10T21:56:21.027

Reputation: 11

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.

Peter Dietz

Posted 2012-03-10T21:56:21.027

Reputation: 151

Thanks! This was all I needed to add to my .zshrc to get visual mode to actually be visible. – danmcardle – 2016-05-05T14:30:49.950

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.

evilguc

Posted 2012-03-10T21:56:21.027

Reputation: 101