在 gnome-terminal 和 konsole 中,我的光标颜色根据 vim 中选定的前景色而变化(如果我使用红色文本,则我的光标为红色)。 在 xterm 中,我的光标始终是白色的。如何将 xterm 设置为与 gnome-terminal 或 konsole 相同的行为?
非常感谢。
最佳答案
根据Xterm Control Sequences ,您可以使用Control Sequence Introducer
和Operating System Command
,简称CSI
/OSC
。
有趣的OSC
:
OSC Ps ; Pt BEL
...
Ps = 1 2 ⇒ Change text cursor color to Pt.
使用此OSC
,我们可以将文本光标更改为任何所需的颜色,它接受颜色名称,例如red
、green
等例如:rgb:RR/GG/BB
参见 XParseColor
有趣的CSI
:
CSI Ps SP q
Set cursor style (DECSCUSR), VT520.
Ps = 0 ⇒ blinking block.
Ps = 1 ⇒ blinking block (default).
Ps = 2 ⇒ steady block.
Ps = 3 ⇒ blinking underline.
Ps = 4 ⇒ steady underline.
Ps = 5 ⇒ blinking bar, xterm.
Ps = 6 ⇒ steady bar, xterm.
现在使用 vim 的 t_SI
和 t_EI
以及这些 CSI
/OSC
我们可以做这样的事情:
let &t_SI="\<Esc>[5 q\<Esc>]12;" . rgb_or_name_color . "\x7"
let &t_EI="\<Esc>[5 q\<Esc>]12;" . rgb_or_name_color_for_not_insert_mode . "\x7"
这是一个函数,用于解析 guifg
的当前配色方案的 Cursor
和 Type
hi 组(对于set termguicolors
)/ctermfg
颜色,然后根据这些颜色设置t_SI
和t_EI
:
fun! UpdateTermCursor()
if &term =~ "xterm\\|rxvt"
fun! s:cologet(gr)
let cc = execute('hi ' . a:gr)
let color = matchstr(cc, 'guifg=\zs[^ ]*')
if color == 'bg'
let color = matchstr(cc, 'guibg=\zs[^ ]*')
endif
if color == ''
let color = matchstr(cc, 'ctermfg=\zs[^ ]*')
endif
if color[0] == '#'
let color = 'rgb:' . substitute(color[1:-1], '..', '\0\/', 'g')[0:-2]
endif
return color
endfun
let cur = s:cologet('Cursor')
" You can change group, for example CursorLineNr, Special, NonText etc.
let curb = s:cologet('Type')
let &t_SI="\<Esc>[5 q\<Esc>]12;" . curb . "\x7"
let &t_EI="\<Esc>[1 q\<Esc>]12;" . cur . "\x7"
" Redraw
:call feedkeys("i\<C-O>:stopinsert\<CR>")
" silent! exe ':redraw!'
" :call feedkeys("i\<Esc>l")
" Restore cursor to I when leaving vim
augroup TermCursorLeave
autocmd!
autocmd VimLeave * silent !echo -ne "\e[5 q"
augroup END
endif
endfun
使用 :colorscheme
更改颜色方案后,执行 :call UpdateTermCursor()
,示例如下:
关于vim - xterm 中的光标颜色;根据 vim 中的语法进行相应更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14920634/