Skip to content

Commit

Permalink
Vim: Use 24-bit true color (termguicolors) whenever possible
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wookayin committed Oct 4, 2017
1 parent 4bad77a commit 2669be4
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions vim/vimrc
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,11 @@ xnoremap <silent> <leader>ag y:Ag <C-R>"<CR>
nmap <leader>cd :cd %:p:h<CR>
" <leader>r : screen sucks, redraw everything
nnoremap <leader>r :redraw!<CR>
function! Redraw()
redraw!
call s:auto_termguicolors() " re-detect true colors
endfunction
nnoremap <leader>r :call Redraw()<CR>
" <leader>src : source ~/.vimrc
nnoremap <leader>src :source ~/.vimrc<CR>
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2669be4

Please sign in to comment.