256 color support for vim background in tmux
Solution 1
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
Solution 2
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.
Solution 3
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.
Solution 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
Solution 5
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.
Related videos on Youtube
winchendonsprings
Updated on September 18, 2022Comments
-
winchendonsprings almost 2 years
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
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?
-
Heptite over 12 yearsWhat happens if you press control-l in normal mode, or execute the ":redraw" command?
-
winchendonsprings over 12 years@Heptite nothing happens still the color change is per line like in the screenshot
-
winchendonsprings over 12 yearsWell 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
-
Andrew T over 12 yearsI 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.
-
Andrew T over 12 yearsAlso, 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. -
oz123 about 11 yearsthis the only thing that finally worked for me!
-
phantomwhale over 10 yearsAll 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.
-
erran over 9 yearsI 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. -
asymmetric about 9 yearsTry
if [[ $TERM == xterm ]]; then TERM=xterm-256color; fi
, it worked for me. -
botimer almost 9 yearsThis is essential when using PuTTY. All the TERM combinations fall short when the colorscheme has a background. Great addition to the thread.
-
Erwin Rooijakkers over 8 years
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. -
danmcardle about 8 yearsThanks! This was all I needed to add to my
.zshrc
to get visual mode to actually be visible. -
HorseHair over 7 yearsThis 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.)
-
Franklin Yu over 4 yearsNo. 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 over 4 years@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. -
Marcos Serpa over 3 yearsI use ZSH so just to put
export TERM="screen-256color"
on .zshrc did the magic. :)