突出显示文本时更改光标背景

Changing cursor background when highlighting text

我通过 Ubuntu 终端使用 vim。我想在选择测试时更改光标的光标背景颜色。

文本选择将使用:

hi Visual       ctermfg=52   ctermbg=167  cterm=none

然而,这不会改变光标的背景,它也被选中但不会给人留下那种印象。

有没有我可以用来执行此操作的命令?我试过了

hi VisualCursor ...  
hi CursorVisual ...

但运气不好。

如评论中所述,在终端中,光标的foreground/background颜色大部分不在vim的控制。 ctermfgctermbg 的设置仅适用于 text foreground/background。请参阅 vim documentation 中标题为 “2. highlight arguments for color terminals”的部分,其中描述了这些。

这些设置对应于 ANSI 转义序列 (ECMA-48)。 光标颜色没有相应的标准:

  • 许多终端允许您指定光标颜色(在启动终端之前),
  • 一些终端识别用于设置光标颜色的转义序列,
  • 一些终端会更改光标颜色以使其在相同颜色的文本上保持可见。

同样,当您 select 使用鼠标输入文字时,光标颜色变化(或不变化)的方式也没有标准:

  • 一些终端在 reverse-video、
  • 中为 selection 着色
  • 一些使用特殊的selection-color,并且
  • selection-colormay/may不影响光标颜色

没有提到实际使用的终端;假设默认终端是 gnome-terminal。所有相关功能也都在 VTE library. Some of this changes with time, but in current code the place to look is in vtegtk.cc (though it helps to read vte.cc 中):

    /**
     * vte_terminal_set_color_cursor:
     * @terminal: a #VteTerminal
     * @cursor_background: (allow-none): the new color to use for the text cursor, or %NULL
     *
     * Sets the background color for text which is under the cursor.  If %NULL, text
     * under the cursor will be drawn with foreground and background colors
     * reversed.
     */
    /**
     * vte_terminal_set_color_cursor_foreground:
     * @terminal: a #VteTerminal
     * @cursor_foreground: (allow-none): the new color to use for the text cursor, or %NULL
     *
     * Sets the foreground color for text which is under the cursor.  If %NULL, text
     * under the cursor will be drawn with foreground and background colors
     * reversed.
     *
     * Since: 0.44
     */
    /**
     * vte_terminal_set_color_highlight_foreground:
     * @terminal: a #VteTerminal
     * @highlight_foreground: (allow-none): the new color to use for highlighted text, or %NULL
     *
     * Sets the foreground color for text which is highlighted.  If %NULL,
     * it is unset.  If neither highlight background nor highlight foreground are set,
     * highlighted text (which is usually highlighted because it is selected) will
     * be drawn with foreground and background colors reversed.
     */

VTE documentation 是从注释生成的(在本例中是模仿 JavaDoc)。所以阅读源代码是理解程序做什么的唯一途径。评论同意 OP 提出的附加问题:

How comes the cursor background color changes when selecting text then? Thing is, the bg color changes to the color of the text the cursor is on.

VTE 中的各个函数模仿 xterm 的功能,它提供了对颜色管理方式的更多控制。例如,VTE 可以识别允许 xtermcontrol 从 shell 命令更改光标颜色的控制序列。控制序列记录在 XTerm Control Sequences:

        Ps = 1 2  -> Change text cursor color to Pt.

这样做会覆盖评论中提到的 reverse-video 方案。引用 xterm manual 给出了一些可以在 VTE 中实施的替代方案的想法:

   highlightColor (class HighlightColor)
           Specifies  the  color  to  use  for  the background of selected
           (highlighted) text.   If  not  specified  (i.e.,  matching  the
           default  foreground),  reverse  video  is used.  The default is
           "XtDefaultForeground".

   highlightColorMode (class HighlightColorMode)
           Specifies whether xterm should use highlightTextColor and high-
           lightColor  to override the reversed foreground/background col-
           ors in a selection.  The default is  unspecified:  at  startup,
           xterm checks if those resources are set to something other than
           the default foreground and  background  colors.   Setting  this
           resource disables the check.