258

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.

Blaszard
  • 30,954
  • 51
  • 153
  • 233
tristen
  • 4,605
  • 4
  • 24
  • 19

26 Answers26

282

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.

Seyeong Jeong
  • 10,658
  • 2
  • 28
  • 39
  • 24
    I use this instead of `tmux -2` as this is more verbose. – thameera Apr 16 '13 at 07:01
  • 105
    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: http://rhnh.net/2011/08/20/vim-and-tmux-on-osx – waffl May 22 '13 at 00:26
  • 8
    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](http://unix.stackexchange.com/questions/118806/tmux-term-and-256-colours-support) – Gilles 'SO- stop being evil' Mar 11 '14 at 21:33
  • 5
    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! – marcelocra Sep 27 '14 at 23:34
  • 11
    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`. – CatDadCode Nov 03 '14 at 17:35
  • 1
    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. – erran Jan 17 '15 at 19:43
  • To confirm, just adding `set -g default-terminal "screen-256color"` to `.tmux.conf`worked for me. Thanks. – PhilT Dec 02 '15 at 10:28
  • 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'` – vesperto Mar 31 '17 at 10:55
  • 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... – Blaszard Jul 05 '18 at 19:22
  • @seyeong-jeong Can you kindly edit the answer to include [this answer](https://stackoverflow.com/questions/10158508/lose-vim-colorscheme-in-tmux-mode#comment42029406_10264470)? Would've saved me some time if I saw this earlier. Thanks! – Richard Domingo Feb 06 '19 at 04:07
  • 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. – Saeed Ahadian Feb 13 '20 at 11:10
  • What do I do if this, nor any other the other answers work. – Jon Deaton Oct 29 '22 at 04:24
  • In my case I would get `missing or unsuitable terminal: screen-256color-bce` when doing that. I solved it by using `alias tmux="TERM=xterm-256color tmux"` instead. – Nicolas Seiller Nov 11 '22 at 17:18
  • Why would you first specify one TERM when launching tmux and then another in the configuration. Default-terminal should be a variant of screen or tmux to work correctly – CervEd May 30 '23 at 08:55
116

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"
sohnryang
  • 725
  • 2
  • 13
  • 27
ElefEnt
  • 2,027
  • 1
  • 16
  • 20
  • I am using tmux via SSH and this is also what worked for me even though I am not using xterm, just regular SSH. – armen.shimoon Apr 30 '14 at 07:02
  • 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. – CatDadCode Sep 19 '14 at 00:06
  • This works for me (MacOS Mojave + iTerm2/Alacritty), thanks – Adrian Magdas Mar 21 '19 at 09:38
  • 2
    Works on Ubuntu 20.04 – sammy Jul 07 '20 at 10:52
  • 1
    Indeed, this is the correct solution, particularly on Ubuntu 20.04 – fracca Jul 13 '20 at 16:21
  • ⚠️ See @Chev 's answer below: https://stackoverflow.com/a/25940093/931156 – lajarre Sep 18 '20 at 15:50
  • This worked for me, using iTerm2 on macOS Catalina. – ruohola Oct 31 '20 at 21:23
  • 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. – G-J Mar 29 '22 at 16:33
  • This pretends your terminal emulator is no longer a tmux or screen instead of actually solving the problem. From the tmux manual "For *tmux* to work correctly this **must** be set to 'screen', 'tmux' or derivative of them". At least change it to `screen.xterm-256color` – CervEd May 30 '23 at 08:51
81

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 :)

Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
tristen
  • 4,605
  • 4
  • 24
  • 19
  • Actually, it's not mandatory at all: I don't use `-2` but I have 256 colors working in Vim in tmux. – romainl Apr 15 '12 at 18:16
  • @romainl It's necessary if tmux doesn't detect your terminal as having 256 colors, which is fairly common. – Gilles 'SO- stop being evil' Mar 11 '14 at 21:34
  • 1
    @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"`. – romainl Mar 11 '14 at 22:28
  • @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). – Gilles 'SO- stop being evil' Mar 11 '14 at 22:30
  • 1
    @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. – romainl Mar 11 '14 at 22:45
  • @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). – Gilles 'SO- stop being evil' Mar 11 '14 at 22:54
  • @Gilles, which is almost exactly my point except that -2 is pointless since it's so easy to – romainl Mar 11 '14 at 23:31
37

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.

CatDadCode
  • 58,507
  • 61
  • 212
  • 318
  • 1
    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. – CatDadCode Nov 03 '14 at 17:34
  • 3
    me too! i would have spent less time on this if this answer were farther up. – Sigfried Mar 24 '18 at 12:15
20

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"

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

pynexj
  • 19,215
  • 5
  • 38
  • 56
Jpatrick
  • 386
  • 2
  • 6
  • 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. – Jpatrick Dec 16 '13 at 17:03
  • 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. – agenteo Mar 27 '14 at 13:05
18

I needed vim to display correctly with tmux in terminal on ubuntu and cygwin/mintty on windows. I got it to work by combining the answers like this.

In .bashrc:

alias tmux="tmux -2"

In .vimrc:

" use 256 colors in terminal
if !has("gui_running")
    set t_Co=256
    set term=screen-256color
endif

" fix cursor display in cygwin
if has("win32unix")
    let &t_ti.="\e[1 q"
    let &t_SI.="\e[5 q"
    let &t_EI.="\e[1 q"
    let &t_te.="\e[0 q"
endif

based on answers from this question, this Vim wiki page and this block cursor question

Community
  • 1
  • 1
Andrew Johnston
  • 1,109
  • 6
  • 11
  • 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. – trigoman Apr 12 '16 at 23:42
  • @trigoman , same for me too, i just had to update the .vimrc. – Masood Alam May 12 '16 at 13:19
  • 1
    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. – Mike Aug 05 '17 at 22:09
13

In case anyone needs 24-bit color support:

Tmux supports 24-bit color as of version 2.2. If your terminal supports 24-bit color, add your terminal to the terminal-overrides setting. For example,

set -ga terminal-overrides ",xterm-256color:Tc"

My environment checklist:

  • macOS Sierra 10.12.3
  • iTerm2 3.0.14 (Report Terminal Type is xterm-256color)
  • neovim 0.1.7 (Enable 24-bit color by adding: xterm-256color to .vimrc)
  • tmux 2.3 (Add set -ga terminal-overrides ",xterm-256color:Tc" to .tmux.conf)

Nothing else I need to add in .bashrc or .zshrc.

Hope this could help.

Yanzhe Chen
  • 427
  • 3
  • 9
  • only thing that worked for me. what exactly does this do? – volk Sep 20 '18 at 20:26
  • It enables true color support. The current recommendations have changed for 3.2 (`set -as terminal-features ",xterm-256:RGB"`) https://github.com/tmux/tmux/wiki/FAQ#how-do-i-use-rgb-colour – markson edwardson Jun 02 '21 at 10:16
13

Since this is the first result on Google and since none of the above helped..wanted to post this so that someone might find it helpful

in .vimrc:

set background=dark
set t_Co=256

HTH

HK_
  • 320
  • 3
  • 8
10

I have tried all of the instructions above, and I found out the most important thing is I have to explicitly add the following line in my .bashrc file.

export TERM=screen-256color

I don't know why alias tmux="TERM=screen-256color-bce tmux" doesn't work out. I use Sierra 10.12.1.

Lion Lai
  • 1,862
  • 2
  • 20
  • 41
8

Just had to deal with this problem, and although all previously posted answers were helpful, they did not solve the issue in my case.

My problem was fixed by removing the following line in my .vimrc:

set termguicolors

which was redondant with another instruction anyway.

Now, with the following line found in previous anwsers in my .tmux.conf:

export TERM="screen-256color"

all is nice and colorful.

Nitlev
  • 119
  • 1
  • 4
5

I'm using gnome terminal and this solved the problem, but (0) don't forget to:

killall tmux

(1) Edit .tmux.conf

# 24 bit color
set -g default-terminal "tmux-256color"
set -ga terminal-overrides ",*256col*:Tc"

(2) Edit: .vimrc

" Enable true color
if exists('+termguicolors')
  let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
  let &t_8b = "\<Esc>[48;2;%lu;%lu;%lum"
  set termguicolors
endif

Solution posted by rinetd on tmux repo issue: https://github.com/tmux/tmux/issues/1246

Karolius
  • 543
  • 1
  • 6
  • 12
4

Adding the following line in .tmux.conf worked for me with macOS Sierra 10.12.6,

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

Zack Zhu
  • 378
  • 4
  • 8
3

If you use tmuxinator or mux, you will need to add these in .bashrc or .zshrc:

alias tmux='TERM=screen-256color tmux -2'
alias tmuxinator='TERM=screen-256color tmuxinator'
alias mux='TERM=screen-256color mux'

These forces to use 256-color in terminal.

Then tmux, tmuxinator and mux command will all work.

qingxp9
  • 141
  • 1
  • 5
3

If you find yourself in the same sitation as me, that none of the above worked.. try this:

in .tmux.conf:

set -g default-terminal "xterm"

in bash config (maybe .bashrc or .bash_profile):

alias tmux="tmux -2"

and then run:

killall tmux

and then relaunch tmux

sohnryang
  • 725
  • 2
  • 13
  • 27
eliasbagley
  • 1,237
  • 9
  • 17
3

I've removed the line set termguicolors, but it doesn't work. Setting set notermguicolors instead in the .vimrc works.

alextanhongpin
  • 607
  • 1
  • 8
  • 11
2

From the tmux man page, adding flag as following:

tmux -2

This -2 flag forces tmux to run on 256-color mode.

This works for me

sohnryang
  • 725
  • 2
  • 13
  • 27
RyanLiu
  • 1,557
  • 2
  • 18
  • 27
2

In order to set up correct color and to get rid of rendering issue in both Ubuntu and Mac:

check this

Community
  • 1
  • 1
Munish
  • 868
  • 9
  • 13
2

Using these two lines in my .tmux.conf worked for me, I'm using Ubuntu 20.04 with Alacritty.

set  -g default-terminal "tmux-256color"
set -ag terminal-overrides ",alacritty:RGB"

Found them in an issue on alacitty's repo, in the comment provided by YodaEmbedding:

https://github.com/alacritty/alacritty/issues/109

1

I didn't have much luck trying to export a different TERM or set default-terminal in my .tmux.conf.

My solution for fixing vim colors within tmux was adding this line to .tmux.conf:

set -g terminal-overrides 'xterm:colors=88'

I'm not sure why this override was needed given that my TERM=xterm and I have iTerm set to xterm as well, but it seems to work.

sohnryang
  • 725
  • 2
  • 13
  • 27
Patrick Lewis
  • 2,131
  • 2
  • 17
  • 16
1

I am using Ubuntu bionic 18.04.4 LTS and tmux 2.6. I have the same issue and it can be resolved by simply adding this into .bashrc

export TERM=screen-256color

And ofcourse don't forget to source it. source ~/.bashrc

Or just restart your teminal

1

Assuming that you already have vim colors matching with your terminal:

  1. Kill all tmux sessions running tmux kill-server

  2. Create a user configuration file for tmux in ~/.tmux.conf

  3. Add to the .tmux.conf the following lines:
    set -g default-terminal "tmux-256color"
    set -ga terminal-overrides ",xterm-termite:Tc"

  4. Run in your command line echo $TERM

  5. Replace xterm-termite with the output returned by echo $TERM

1

ENV:

Fedora 29 workstation x86_64, GNOME Terminal 3.30.1

VIM - Vi IMproved 8.1 (2018 May 18, compiled Mar 8 2019 09:25:44)

GNU bash, version 4.4.23(1)-release (x86_64-redhat-linux-gnu)

tmux 2.7

vim is using solarized scheme.

.vimrc

...
let g:solarized_termcolors=256
let g:solarized_termtrans=1

syntax enable
set background=dark
colorscheme solarized
...

The value of $TERM in bash is:

[u@loc ~]$ echo $TERM
xterm-256color
[u@loc ~]$ tput colors
256

Mehthod 1: works.

Check $TERM value inside tmux session. Get

[u@loc ~]$ echo $TERM
screen
[u@loc ~]$ tput colors
8

So, just set export TERM=screen-256color inside tmux session. This method only works in the current pane of the session.


Method 2: works.

Create ~/.tmux.conf file and add set -g default-terminal "tmux-256color" to the file.

or Just run echo "set -g default-terminal \"tmux-256color\"" > ~/.tmux.conf

Then kill all tmux sessions.

Start a new session and check the $TERM value inside tmux session. Get

[u@loc ~]$ echo $TERM
tmux-256color
[u@loc ~]$ tput colors
256

And vim colorized scheme works fine for all pane and all tmux sessions.

I also tried xterm-256color and screen-256color in ~/.tmux.conf. They all work fine for colorizing the vim scheme.

By the way, I don't have any configurations being related to this setting in my ~/.bash_profile, ~/.bashrc and ~/.vimrc.

See also https://github.com/tmux/tmux/wiki/FAQ#how-do-i-use-a-256-colour-terminal, https://github.com/tmux/tmux/wiki/FAQ#why-do-you-use-the-screen-terminal-description-inside-tmux

Other useful talks are Vim color scheme changes in tmux #699, 256-color-support-for-vim-background-in-tmux, getting-256-colors-to-work-in-tmux, tmux-term-and-256-colours-support

Nick Dong
  • 3,638
  • 8
  • 47
  • 84
0

I use fedora 34.

I add this to .tmux.conf file

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

Then add this to .vimrc

set background=dark
set t_Co=256
Yudi Krisnandi
  • 417
  • 1
  • 5
  • 12
0

I am using WSL2 to run ubuntu 20.04 on windows 11.

I recently changed from bash to zsh(with oh-my-zsh) to achieve more customization. Downloaded some vim color-schemes that utilise italics font. Most of them from https://vimcolorschemes.com/.

When I use vim in tmux the appearance is messed up completely.

I managed to have tmux vim to look exactly the same like run normally.

Below is my config:

In .vimrc add these lines:

 if exists('+termguicolors')
        let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
        let &t_8b = "\<Esc>[48;2;%lu;%lu;%lum"
        set termguicolors
  endif

In .tmux.conf add these lines:

set -g default-terminal "tmux-256color"
set -g default-terminal "tmux-256color-italic"
set -ga terminal-overrides ",tmux-256color:Tc"

This will allow colored and italic fonts in tmux terminal.

Or you can use echo $TERM to find out your terminal config. It should be something like xterm-256color or screen-256color. Use them to replace the tmux-256color in the code above. I did not find it necessary, though.

izzie
  • 1
  • 2
0

You likely experiencing one of two problems, and possibly both at the same time.

default-terminal

  1. The value of $TERM in tmux doesn't support color, or not the colors you need. Perhaps it's screen, which is monochrome.

Change this by modifying the default-terminal. Possibly using

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

or screen.xterm-256color, screen-256color etc.

set termguicolors

  1. The vim color settings are incompatible with the terminal and/or tmux.

In order to use truecolor in vim you usually

set termguicolors

but this only works nicely with xterm*. Use this in your .vimrc to play nicely with xterm, as well as other screen, tmux variants.

if !has('gui_running') && &term =~ '\%(screen\|tmux\)'
  let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
  let &t_8b = "\<Esc>[48;2;%lu;%lu;%lum"
endif
set termguicolors

The check in term should be a bit more elaborate, to actually match variants with color but this should work for most setups.

Alternative approaches

  • Pretend tmux is xterm, it's not. Both top answers suggest this approach but it's stated in the manual

For tmux to work correctly, this must be set to ‘screen’, ‘tmux’ or a derivative of them.

  • Disable vims truecolor support. This shouldn't be needed but sure, you can disable 24-bit color support and just use 256 colors.
CervEd
  • 3,306
  • 28
  • 25
0

Inside Vim you can check the term variable:

:set term?

Do this inside tmux and outside, and compare them. Here I put this line inside .vimrc to solve the problem:

set term=xterm-256color

And now, both inside and outside tmux, the term variable is the same.