Use the system’s terminal capabilities database, as I’m in the unusual situation where |vim|’s are inferior on many of the systems I regularly use:
set nottybuiltin
Annoying, but necessary, to refresh termcap:
set term=$TERM
We’ll forcibly set 'guifont'
here to make it easier to configure options
based on the used font:
silent let &guifont = system('fc-match -f "%{family}" monospace')
Note
This obviously doesn’t work if you configure custom fonts for your terminal, but I configure a global monospace font and use that everywhere. If the font isn’t correct in one application, it is usable for other applications. The uniformity is the only thing that matter to me.
.. todo:: Some terminals support querying the current font using escape sequences, we should look in to which terms can reliably do this. For example, :command:`xterm` requires additional configuration to support the escape.
Where possible, enable 24-bit colour:
if has('termguicolors') if &t_Co > 256 || &term =~# '^\(kitty\|rxvt-unicode.*\|st\)$' set termguicolors endif endif
Poke around, as best we can, to discern the background colour:
const s:feature_terms = \ '^\(kitty\|linux\|\(rxvt-unicode\|st\|xterm\)\(-256color\)\?\)$' if &term =~# s:feature_terms || split($COLORFGBG .. ';padding', ';')[0] == 15 if &background !=# 'dark' set background=dark endif else if &background !=# 'light' set background=light endif endif
Note
We purposely check that we’re changing the background setting, as it can
cause an ugly redraw or reissue :autocmd
s.
… and change the cursor colour depending on mode on supported terminals:
if &term =~# s:feature_terms && exists('&t_SI') let &t_EI = "\<Esc>]12;green\x7" let &t_SI = "\<Esc>]12;purple\x7" let &t_SR = "\<Esc>]12;red\x7"
.. todo:: This is brittle, but I don’t know a foolproof way to handle it. Thoughts?
… and match cursor types to :command:`gvim`:
let &t_EI ..= "\<Esc>[2 q" let &t_SI ..= "\<Esc>[6 q" let &t_SR ..= "\<Esc>[4 q" endif
Omnicompletion rocks, but doesn’t:
if has('insert_expand') inoremap <Nul> <C-x><C-o> endif
kitty and |vim| interact poorly with background repainting, the following works around it:
if $TERM ==# 'xterm-kitty' let &t_ut='' endif
kitty has some really cool features, called kittens, that can do all kinds of magic; :command:`icat`, for example, can display images inline. However, |vim| works in the alternate screen where the images will not be displayed. The following snippet disables the alternate screen, which is a poor solution to workaround this:
if $TERM ==# 'xterm-kitty' set t_ti= t_te= endif
kitty and VTE-based terminals can display undercurls for spelling error highlighting that more closely matches my setup in :command:`gvim`:
if $TERM ==# 'xterm-kitty' || exists('$VTE_VERSION') let &t_Cs = "\e[4:3m" let &t_Ce = "\e[4:0m" highlight clear SpellBad highlight SpellBad ctermfg=1 term=undercurl cterm=undercurl augroup jnrowe_kitty autocmd! autocmd ColorScheme * highlight clear SpellBad | \ highlight SpellBad ctermfg=1 term=undercurl cterm=undercurl augroup END endif
.. spelling:: termcap undercurls