lose vim colorscheme in tmux mode

103,305

Solution 1

I had the same problem. Only difference was I am using solarize rather then molokai.

To fix the issue, I have set up an alias in ~/.bashrc:

alias tmux="TERM=screen-256color-bce tmux"

And set up the default-terminal option in ~/.tmux.conf:

set -g default-terminal "xterm"

Lastly, do $ source ~/.bashrc to load new alias.

Solution 2

I tried all the solutions above and what finally worked for me is putting the following lines in .tmux.conf:

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

Solution 3

As @romainl mentions above, I needed to force tmux to use 256 colors by adding the -2 flag:

$ tmux -2

I added alias tmux='tmux -2' to my bash_profile, so, I don't forget :)

Solution 4

I just discovered why I was having a lot of confusion. I, like others here, was having a difficult time getting the default-terminal setting to take effect. I remembered that I had a tmux session in the background. I re-attached my session, closed out my processes, and closed ALL tmux processes. The next time I restarted tmux the default-terminal setting in .tmux.conf began to take effect. I don't know if others are doing this as well but I recommend closing all tmux processes before modifying the .tmux.conf file.

I got my setup to work on my local machine (OSX 10.9.5 with iTerm2) without any modification to .bashrc or .bash_profile. All I did was add the line set -g default-terminal "xterm-256color" to ~/.tmux.conf and restarted all tmux processes.

I got my remote setup (ssh to Ubuntu 14.04) to work exactly the same way without any modifications to .bashrc. I simply added set -g default-terminal "xterm-256color" to ~/.tmux.conf on my remote machine and restarted all remote tmux processes.

You can test what Vim is seeing by doing echo $TERM from within a tmux session. It kept saying screen as the value until I restarted all tmux processes, at which point it reflected xterm-256color as expected.

Hope that helps.

Solution 5

So this a bit on the stale side, but it's might be worth mentioning that using screen will often break the Home and End keys. Using

export TERM="xterm-256color"

in should keep the functionality of these and allow the color scheme (or powerline) to work fine.

Share:
103,305

Related videos on Youtube

tristen
Author by

tristen

A developer and designer residing between Washington, DC and Toronto, Ontario

Updated on September 28, 2021

Comments

  • tristen
    tristen over 2 years

    I'm running iterm2 and when I'm in tmux mode the colorscheme I have set in vim does not show up. Only the color scheme I've set in iterm. If I run vim from shell the colorscheme appears correct - its only when I'm in tmux mode.

    I've tried setting :colorscheme molokai when in vim (see screenshot below) and it doesn't change - again, the default colorscheme for iterm2 remains.

    Am I missing some setting to iterm or tmux.conf? My dotfles are up on github here.

    • romainl
      romainl about 12 years
      What happens when you do $ tmux -2?
    • tristen
      tristen about 12 years
      That was totally it. What's the -2 flag?
    • romainl
      romainl about 12 years
      It forces tmux to work with 256 colors.
    • rodorgas
      rodorgas over 3 years
      @romainl something good happens :)
  • romainl
    romainl about 12 years
    Actually, it's not mandatory at all: I don't use -2 but I have 256 colors working in Vim in tmux.
  • thameera
    thameera about 11 years
    I use this instead of tmux -2 as this is more verbose.
  • waffl
    waffl almost 11 years
    Personally I had to use set -g default-terminal "screen-256color" to work rather than xterm on OS X, sshed into an Ubuntu box. I referred to this site: rhnh.net/2011/08/20/vim-and-tmux-on-osx
  • Jpatrick
    Jpatrick over 10 years
    Thanks for pointing this out. I didn't know that. It still seems to work for Debian derv from what I've seen so far. In my defence the post was in reference to OSX.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 10 years
    The alias here doesn't make sense: it's pretending to tmux that it's running inside screen or tmux, which is hardly ever the case. To tell tmux to assume that the terminal it's running in supports 256 colors, run tmux -2 or tmux -2 attach (tmux decides each time you attach a new or existing session to a terminal). A sensible alias would be alias tmux='tmux -2'. See also tmux, TERM and 256 colours support
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 10 years
    @romainl It's necessary if tmux doesn't detect your terminal as having 256 colors, which is fairly common.
  • romainl
    romainl about 10 years
    @Gilles, Vim doesn't detect that the terminal supports 256 colors because tmux's default TERM is screen. -2 doesn't change TERM and doesn't change anything color-related to what info Vim gets from its environment: :echo &t_Co still returns 8. With the info it is given, Vim does the right thing. The only things that must be set are 1. your terminal emulator's TERM to xterm-256color or an equivalent value like urxvt-unicode-256color and 2. tmux's default TERM with set -g default-terminal "screen-256color".
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 10 years
    @romainl Most environments have TERM set to xterm, not xterm-256color, hence the need to run tmux -2 (or TERM=xterm-256color tmux, or change the termcap or other ways of accomplishing the same thing).
  • romainl
    romainl about 10 years
    @Gilles, the problem is that -2 doesn't accomplish anything that has any impact on Vim's behavior regarding 256 color support because the only thing that matters to Vim is your TERM and that's to be set at your end of the chain: in your terminal emulator. If you SSH through 5 hosts and your terminal is set correctly, Vim launched on the 5th host will see your 256 colors TERM and act accordingly. If you use tmux at any point of the chain -2 will have zero impact on Vim's behavior because all it sees is its environment is limited to 8 colors because of tmux's default TERM: screen, -2 or not.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 10 years
    @romainl You need to take care of both the outside (-2 or TERM=xterm-256color or setting the xterm termcap/terminfo to declare 256 colors) and the inside (set -g default-terminal "screen-256color" or setting the screen termcap/terminfo to declare 256 colors).
  • romainl
    romainl about 10 years
    @Gilles, which is almost exactly my point except that -2 is pointless since it's so easy to
  • agenteo
    agenteo about 10 years
    tmux 1.9a, OSX 10.9.2, iTerm2 Build 1.0.0.20130622 export TERM="xterm-256color" in .bash_profile, then set iterm to xterm-256-color worked for me. No need to set any tmux configuration.
  • armen.shimoon
    armen.shimoon almost 10 years
    I am using tmux via SSH and this is also what worked for me even though I am not using xterm, just regular SSH.
  • Chev
    Chev over 9 years
    I wasn't using it over ssh and this was the only answer that worked for me. Local iTerm session in OSX 10.9.5.
  • marcelocra
    marcelocra over 9 years
    I read in some other answer (couldn't find the reference) that it was not a good practice to force the TERM, though I don't know for sure. Since the tmux -2 option with the set -g default-terminal "screen-256color" worked for me, that will be my choice. But thanks anyway!
  • Chev
    Chev over 9 years
    I've confirmed on several more operating systems since this post, closing all processes and setting the default-terminal option in .tmux.conf has worked 100% of the time so far.
  • Chev
    Chev over 9 years
    If the .tmux.conf setting doesn't appear to be taking effect, close all tmux processes and restart. You can just do set -g default-terminal "screen-256color" without setting any alias in .bashrc. I think people are doing workarounds like the environment variable alias because they aren't closing all tmux processes after configuring .tmux.conf.
  • erran
    erran over 9 years
    I ended up exporting TERM as xterm-256color and adding set -g default-terminal "xterm-256color" to my tmux config. In my OS X terminal profile I ensured that the terminal was declared as xterm-256color as well. I ensured that my terminal profile was set to the same TERM type. This works preserves my fish shell and vim themes.
  • PhilT
    PhilT over 8 years
    To confirm, just adding set -g default-terminal "screen-256color" to .tmux.confworked for me. Thanks.
  • trigoman
    trigoman about 8 years
    I had the issue with mintty, and adding the vim configuration fixed it for me. I didn't even need to do the tmux alias.
  • Masood Alam
    Masood Alam almost 8 years
    @trigoman , same for me too, i just had to update the .vimrc.
  • vesperto
    vesperto about 7 years
    The default-terminal here didn't help me, vim kept starting up with weird bindings (i.e. Home/End keys switching to insert mode and inserting F and H plus newline). I set an alias in my .bashrc instead: alias vim='TERM=xterm vim'
  • Mike
    Mike over 6 years
    I tried all of the previous suggestions. Just added set t_Co=256 to .vimrc and it was fixed. I didn't need to create a tmux alias.
  • Adam Erickson
    Adam Erickson over 6 years
    Same here. Thank you @waffl for finding this solution!
  • Sigfried
    Sigfried about 6 years
    me too! i would have spent less time on this if this answer were farther up.
  • Blaszard
    Blaszard almost 6 years
    This worked perfectly on vim but for some reasons started to be messed up on neovim, especially since all the comment-outs now have background highlight...
  • volk
    volk over 5 years
    only thing that worked for me. what exactly does this do?
  • llinfeng
    llinfeng over 5 years
    Have tested these two lines, and they work well for my Vim on WSL-Windows10.
  • Foobar
    Foobar over 5 years
    Thanks a lot. This worked on Ubuntu 18 with gnome-terminal
  • Richard Domingo
    Richard Domingo about 5 years
    @seyeong-jeong Can you kindly edit the answer to include this answer? Would've saved me some time if I saw this earlier. Thanks!
  • Adrian Magdas
    Adrian Magdas about 5 years
    This works for me (MacOS Mojave + iTerm2/Alacritty), thanks
  • markovchain
    markovchain over 4 years
    This is what worked for me. Just plain export of TERM. +1
  • Saeed Ahadian
    Saeed Ahadian about 4 years
    I'm using CentOS 8 and for me, the right configuration was set -g default-terminal "xterm-256color" in my ~/.tmux.conf. I also didn't need to make any changes to my .bashrc file for it to work.
  • sammy
    sammy almost 4 years
    Works on Ubuntu 20.04
  • fracca
    fracca almost 4 years
    Indeed, this is the correct solution, particularly on Ubuntu 20.04
  • lajarre
    lajarre over 3 years
    ⚠️ See @Chev 's answer below: stackoverflow.com/a/25940093/931156
  • Ambareesh
    Ambareesh over 3 years
    Worked like a charm!
  • ruohola
    ruohola over 3 years
    This worked for me, using iTerm2 on macOS Catalina.
  • JohnnyD27
    JohnnyD27 about 3 years
    This was the only thing that worked for me on RedHat using xterm. Thanks!
  • markson edwardson
    markson edwardson almost 3 years
    It enables true color support. The current recommendations have changed for 3.2 (set -as terminal-features ",xterm-256:RGB") github.com/tmux/tmux/wiki/FAQ#how-do-i-use-rgb-colour
  • LeOn - Han Li
    LeOn - Han Li about 2 years
    This works great for tmux/neovim/alacritty. Thanks a lot!
  • G-J
    G-J about 2 years
    Something recently broke my vim color scheme on a WSL2 installation of Ubuntu on WIndows 11, running inside Windows Terminal. This was the correct fix.
  • dc_Bita98
    dc_Bita98 almost 2 years
    worked for me on Ubuntu 22.04 with gnome