vim - xterm 中的光标颜色;根据 vim 中的语法进行相应更改

标签 vim syntax xterm

在 gnome-terminal 和 konsole 中,我的光标颜色根据 vim 中选定的前景色而变化(如果我使用红色文本,则我的光标为红色)。 在 xterm 中,我的光标始终是白色的。如何将 xterm 设置为与 gnome-terminal 或 konsole 相同的行为?

非常感谢。

最佳答案

根据Xterm Control Sequences ,您可以使用Control Sequence IntroducerOperating System Command,简称CSI/OSC

有趣的OSC:

OSC Ps ; Pt BEL
...
Ps = 1 2 ⇒  Change text cursor color to Pt.

使用此OSC,我们可以将文本光标更改为任何所需的颜色,它接受颜色名称,例如redgreen等例如: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_SIt_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 的当前配色方案的 CursorType hi 组(对于set termguicolors)/ctermfg 颜色,然后根据这些颜色设置t_SIt_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 term cursor color

关于vim - xterm 中的光标颜色;根据 vim 中的语法进行相应更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14920634/

相关文章:

php mysql 检查现有语法

python - 如何知道输出是否进入终端?

vim - 如何退出 Vim?

python - 关于幅度的语法错误

mouse - 在 gvim 中用鼠标重新排列标签

JavaScript 点符号

linux - Vim:映射 ctrl+pgup 和 ctrl+pgdn (CTRL+Page Up/Down) 组合键

ruby - "ruby script.rb"与 "xterm -e ruby script.rb"

windows - 如何在Windows Source Insight中使用vim编辑器

vim 无法将 <C-Tab> 映射到 :tabnext