简体   繁体   中英

vim: termguicolors with xterm-true-color still causing problems

I have seen a multitiude of posts where not enabling 'tgc' with:

if exists('+termguicolors')
   let &t_8f =  "\<Esc>[38:2:%lu:%lu:%lum"
   let &t_8b =  "\<Esc>[48:2:%lu:%lu:%lum"
   " let &t_8f =  "\<Esc>[38:2;%lu;%lu;%lum" ( I have tried both ':' and ';' )
   " let &t_8b =  "\<Esc>[48:2;%lu;%lu;%lum"
   set t_Co=256
   set termguicolors
   " colorscheme gruvbox " color scheme
 endif

causes vim to have weird color output - usually with tmux.

Initially I found success with this too. Until one fine day (seriously,) vim starts behaving weird. but in a different way. It goes monochrome.

If, I don't set it, ie se notgc , I actually get the right colorscheme. This is exactly the same in vim + tmux usage as well.

But, when I use vim + tmux, if I, say, kill a pane, or reload ~/tmux.config, existing vim sessions get weird again!

Here is a look at one such occurance, the left is a newly opened buffer, while the right one, with the same settings, suddenly goes weird.

My settings:

  • This is my company work server, I don't have su permissions.
$ cat /proc/version
Linux version 2.6.32-504.30.3.el6.x86_64 (mockbuild@c6b8.bsys.dev.centos.org) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC) ) #1 SMP Wed Jul 15 10:13:09 UTC 2015
  • VIM
vim --version
VIM - Vi IMproved 8.1 (2018 May 18, compiled Jul 26 2019 22:56:22)
Included patches: 1
Compiled by bob@c6x64
Huge version with GTK2 GUI.  Features included (+) or not (-):
+acl               +farsi             +mouse_sgr         -tag_any_white
+arabic            +file_in_path      -mouse_sysmouse    -tcl
+autocmd           +find_in_path      +mouse_urxvt       +termguicolors
-autoservername    +float             +mouse_xterm       +terminal
+balloon_eval      +folding           +multi_byte        +terminfo
+balloon_eval_term -footer            +multi_lang        +termresponse
+browse            +fork()            -mzscheme          +textobjects
++builtin_terms    +gettext           +netbeans_intg     +timers                                                                                               [25/126]
+byte_offset       -hangul_input      +num64             +title
+channel           +iconv             +packages          +toolbar
+cindent           +insert_expand     +path_extra        +user_commands
+clientserver      +job               -perl              +vertsplit
+clipboard         +jumplist          +persistent_undo   +virtualedit
+cmdline_compl     +keymap            +postscript        +visual
+cmdline_hist      +lambda            +printer           +visualextra
+cmdline_info      +langmap           +profile           +viminfo
+comments          +libcall           -python            +vreplace
+conceal           +linebreak         -python3           +wildignore
+cryptv            +lispindent        +quickfix          +wildmenu
+cscope            +listcmds          +reltime           +windows
+cursorbind        +localmap          +rightleft         +writebackup
+cursorshape       -lua               -ruby              +X11
+dialog_con_gui    +menu              +scrollbind        -xfontset
+diff              +mksession         +signs             +xim
+dnd               +mouse             +startuptime       +xsmp_interact                                                                                         
-ebcdic            +mouseshape        +statusline        +xterm_clipboard
+emacs_tags        +mouse_dec         -sun_workshop      -xterm_save
+eval              +mouse_gpm         +syntax
+ex_extra          -mouse_jsbterm     +tag_binary
+extra_search      +mouse_netterm     +tag_old_static
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
  system gvimrc file: "$VIM/gvimrc"
    user gvimrc file: "$HOME/.gvimrc"
2nd user gvimrc file: "~/.vim/gvimrc"
       defaults file: "$VIMRUNTIME/defaults.vim"
    system menu file: "$VIMRUNTIME/menu.vim"
  fall-back for $VIM: "/tools/apps/local/vim/latest/share/vim"
Compilation: gcc -std=gnu99 -c -I. -Iproto -DHAVE_CONFIG_H -DFEAT_GUI_GTK  -pthread -I/usr/include/gtk-2.0 -I/usr/lib64/gtk-2.0/include -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/pango-1.0 -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freety
pe2 -I/usr/include/libpng12     -O2 -fno-strength-reduce -Wall -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1
Linking: gcc -std=gnu99   -L/usr/local/lib -Wl,--as-needed -o vim   -pthread -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgio-2.0 -lpangoft2-1.0 -lpangocairo-1.0 -lgdk_pixb
uf-2.0 -lcairo -lpango-1.0 -lfreetype -lfontconfig -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lrt -lglib-2.0   -lSM -lICE -lXpm -lXt -lX11 -lXdmcp -lSM -lICE  -lm -lti
nfo -lelf -lnsl  -lselinux -lacl -lattr -lgpm

The default cshrc had TERM explicitly set as vt100 . If I change it to xterm-256color or screen-256color , I actually get good colorschemes within vim, and :set term=$TERM or explicitly to either of the above two, with the same problems showing up.

  • TMUX v2.3, tried the following:
set -g default-terminal "screen/xterm-256color" 
set -ga terminal-overrides ",*256col*:Tc"
  • I use gnome-terminal(v2.31.3), I also tried Konsole(v 2.3.3, Qt: 4.6.2, KDE: 4.3.4) -- and here I am not seeing the issue, But, konsole is terribly slow over my vnc connection, which is not the case gnome-terminal. so I decided to stick with it.

-- On color support: No true color support since

awk 'BEGIN{
    s="/\\/\\/\\/\\/\\"; s=s s s s s s s s;
    for (colnum = 0; colnum<77; colnum++) {
        r = 255-(colnum*255/76);
        g = (colnum*510/76);
        b = (colnum*255/76);
        if (g>255) g = 510-g;
        printf "\033[48;2;%d;%d;%dm", r,g,b;
        printf "\033[38;2;%d;%d;%dm", 255-r,255-g,255-b;
        printf "%s\033[0m", substr(s,colnum+1,1);
    }
    printf "\n";
}

returns this. where as Konsole returns a much smoother output .

Should I ditch gnome-terminal and try some other emulator (which is snappier than Konsole)? I would really appreciate if a solution can be found within gnome-terminal itself.

I hope I have given all the relevant details, Thanks

The following combination should work in most scenarios:

~/.vimrc:

" Inspect $TERM instad of t_Co as it works in neovim as well
if &term =~ '256color'
  " Enable true (24-bit) colors instead of (8-bit) 256 colors.
  " :h true-color
  if has('termguicolors')
    let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
    let &t_8b = "\<Esc>[48;2;%lu;%lu;%lum"
    set termguicolors
  endif
  colorscheme <your favorite colorscheme goes here>
endif
~/.tmux.conf:

# Set $TERM and force 256 colors.
# https://github.com/tmux/tmux/wiki/FAQ#how-do-i-use-a-256-colour-terminal
# https://github.com/tmux/tmux/wiki/FAQ#how-do-i-use-rgb-colour
if "[[ $TERM =~ 256color]]" "
    set -g default-terminal 'tmux-256color';
    set -ga terminal-overrides ',tmux-256color:Tc' "

Note that this doesn't hard-code any color configuration in vim , which is a clean approach. You should never have to force vim to use 256 color. The terminal is responsible to tell vim whether 256 colors are supported or not.

This approach also doesn't hard-code any color configuration in tmux . It may make sense to force tmux to use 256 color in some scenarios, though. If you want to do so, just remove the if :

~/.tmux.conf:

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

I believe this is in part due to your tmux configuration. You'll want to set the following in .tmux.conf :

set -g default-terminal screen-256color
set -ag terminal-overrides ",xterm-256color:Tc"

Note that you cannot use a slash in the default terminal option, since it must be a valid terminal type. I assume all the systems you are logging into have the screen-256color terminal type, but you can check by running infocmp screen-256color , which should print terminal info on success and an error on error. You may need to install additional packages to add that terminal type.

As far as GNOME Terminal, 2.31 is very old. The VTE version used in GNOME 2 definitely does not support true colors. Ideally you'll set up your local system to use a modern terminal emulator and log in over SSH instead of using VNC, and things will work.

I will be clear that since you're using CentOS or RHEL 6 here, that virtually any terminal emulator you're trying to use will likely be broken. This OS is a decade old and will lose security support as of November. Your best bet, as mentioned, is to use a more modern environment on your laptop or desktop and log in over SSH.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM