As you may or may not know, I am a big fan of Vim, Tmux and the Solarized theme. Unfortunately, these three components don’t always go together very well.
As a rule, I tend to use the dark Solarized theme for my terminals, and the light one for my Vim. Why? I don’t know. I prefer to edit text with dark text on a light background, but dark on light terminals look weird to me. Plus, the stark contrast between backgrounds makes it easy to distinguish an editor pane from a shell pane in Tmux.
Sadly, depending on your system, half the time you open Vim inside of Tmux, Solarized theme (both light and dark) look like shit. This only happens when running the console version of vim from inside Tmux. I hardly ever have issues with the Solarized theme in the command line vim when running it directly. But when it is launched from inside of Tmux, the color scheme is off and moving the cursor introduces strange artifacts on the screen.
Why does that happen? I’m not entirely sure. It appears that Tmux happens to be very finicky about terminal types, and tends to default to 16 color mode rather than the 256 color one required to use Solarized themes. Fortunately, you can usually force it to behave properly by following these steps:
First, set your terminal (Konsole, iTerm2, Putty) to identify as:
screen-256color
Why screen? Because Tmux is a screen-like program, but it likes to inherit the terminal type from the terminal that launched it. You could also use xterm-256color but in my testing setting it to screen worked every time, while xterm setting was problematic under some combinations of terminals and shells.
Next, add these two lines to your .bashrc:
export TERM="screen-256color"
alias tmux="tmux -2"
Again, we are once again telling Tmux the terminal type is screen-256color regardless of what the Terminal says. The $TERM environmental variable is the second place where Tmux likes to inherit it’s terminal type from, and if you have a miss-match between the value declared by your actual terminal, and by your shell then Tmux gets confused. The second line forces Tmux to always start in 256 color support mode.
In your .tmux.conf add this line:
set -g default-terminal "screen-256color"
As you can see we are covering all the bases. The point is to give Tmux no choice but to use screen-256color terminal type, regardless of what it might want to do.
Finally, in your .vimrc add these two lines:
set t_Co=256 " force vim to use 256 colors
let g:solarized_termcolors=256 " use solarized 256 fallback
Once you do all of this, the Solarized theme should work from within Tmux. I have tested this setup on Kubuntu with Kterm as well as on Windows with Putty and it worked in both. If you know of a better way of ensuring that Vim + Solarized work with Tmux, please let me know in the comments.
Another approach to this problem is to add to konsole a Solarized color scheme (the default one won’t work, but there are a lot of alternatives). I am using this one with only a small modification. The advantages of this solution are that you also get Solarized in your console, you don’t need to play with configuration files, and you don’t need to force Solarized to use 16 or 256 colors. The drawback is that you’ll be tied to the dark or light version for everything, because any other mix (dark scheme for console/light background for vim, for example) will produce ugly dark/light boxes around some elements in vim. Also, you need to add the color scheme to each console emulator that you use.
I know this is old, but I noticed one problem in my setup. In Ubuntu 12.04, setting TERM=screen-256color causes weirdness when I press the home/end keys on the keyboard in the console (not necessarily in tmux, I didn’t check that). Other weirdness might happen too, but setting TERM=xterm-256color at least solves the home/end weirdness.
Thanks! It was driving me nuts that the combination of tmux, vim and solarized was working sort of ok but failing on vim tabs. The solution works on OSX and iTerm2. yay!
thanks you so much, I spent 2 hours not be able to fix it.
Sweet! Thank you so much for writing this up.