22

I installed tmux in iTerm 2.(Build 3.0.13) When I execute vim in tmux, syntax highlighting looks like this.

no syntax highlighting

But outside tmux, syntax highlighting looks fine.

ordinary syntax highlighting

My $TERM inside and outside tmux is xterm-256color. I also added

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

in .tmux.conf and added this

set t_Co=256
set t_AB=m
set t_AF=m
if &term =~ '256color'
    set t_ut=
endif

in .vimrc.

I also tried tmux -2 command and read these questions.

lose vim colorscheme in tmux mode

Incorrect colors with vim in iTerm2 using Solarized

Is this a problem of tmux, vim, or my configuration?

EDIT:

My .vimrc in GitHub Gist

https://gist.github.com/sohnryang/3c63397f332f2e30c7d7b2a83c3c9f52

sohnryang
  • 495
  • 1
  • 3
  • 14

3 Answers3

15

Well, I solved the problem by myself.

as @Carpetsmoker♦ commented, I started to suspect that my .vimrc is a problem. I read this question and started vim with this command inside tmux.

vim -u NONE -U NONE -N ~/.vimrc

After starting vim with command above, I ran this command inside vim.

:syn on
:colorscheme solarized8_dark

These highlighted my .vimrc file. So, I started to debug my .vimrc.

Long story short, set termguicolors was the problem. If I ran vim with set termguicolors commented in .vimrc, I could see corrected syntax highlighting in tmux.

sohnryang
  • 495
  • 1
  • 3
  • 14
9

From the vim manual: :h termguicolors recommends reading :h xterm-true-color, also see :h $TERM

Sometimes setting 'termguicolors' is not enough and one has to set the |t_8f|
and |t_8b| options explicitly.  [ ... these are]  only set [to some default] when `$TERM` is `xterm`.

I use a condition similar to the below: (nvim recommends not using &term but rather $TERM )

if $TERM =~# '256color' && ( $TERM =~# '^screen'  || $TERM =~# '^tmux' )
    let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
    let &t_8b = "\<Esc>[48;2;%lu;%lu;%lum"
    set termguicolors
endif
ixil
  • 146
  • 1
  • 2
5

I actually made it work just fine with termguicolors. This is what I did 1. in my ~/.bash_profile i put this:

export TERM=xterm-256color

and inside my ~/.vimrc I had this

syntax enable
colorscheme Spacegray

set termguicolors

and it worked perfectly! reference

abbood
  • 733
  • 9
  • 23