From 2669be446f9a61280953001487a9ee6a2185fa0a Mon Sep 17 00:00:00 2001 From: Jongwook Choi Date: Wed, 4 Oct 2017 15:01:34 -0400 Subject: [PATCH] Vim: Use 24-bit true color (termguicolors) whenever possible Whenever possible, use termguicolors features to enable 24-bit true color, even in the terminals like GUI. There is a subtle challenge as some shell or terminal emulators may not support 24-bit colors. We assume that modern terminal emulators we would use will support it, or even fall-back to 256 colors nicely, but otherwise it may look broken as in mosh. For mosh, to workaround this issue until mosh supports true color, we detect whether the vim is running in a mosh environment. This detection should be robust even when it is running under a tmux session; to this end, the 'is_mosh' script is introduced. Whenever the vim is initialized or asked to redraw the screen, we detect mosh under which the feature shall be disabled. --- vim/vimrc | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/vim/vimrc b/vim/vimrc index 48ca5667..8eb13b0c 100644 --- a/vim/vimrc +++ b/vim/vimrc @@ -341,7 +341,11 @@ xnoremap ag y:Ag " nmap cd :cd %:p:h " r : screen sucks, redraw everything -nnoremap r :redraw! +function! Redraw() + redraw! + call s:auto_termguicolors() " re-detect true colors +endfunction +nnoremap r :call Redraw() " src : source ~/.vimrc nnoremap src :source ~/.vimrc @@ -429,9 +433,28 @@ if &term =~ '256color' endif " 24-bit true color: neovim 0.1.5+ / vim 7.4.1799+ -if (has("termguicolors")) && &term == 'xterm-256color' - set termguicolors -endif +" enable ONLY if TERM is set valid and it is NOT under mosh +function! s:is_mosh() + let output = system("is_mosh -v") + if v:shell_error + return 0 + endif + return !empty(l:output) +endfunction + +function s:auto_termguicolors() + if !(has("termguicolors")) + return + endif + + if &term == 'xterm-256color' && !s:is_mosh() + set termguicolors + else + set notermguicolors + endif +endfunction +call s:auto_termguicolors() + " apply base theme silent! colorscheme xoria256