如何更改 tty 中的光标形状?

如何更改 tty 中的光标形状?

我想将 tty 光标形状下划线更改为阻止。我试过这个:

if [[ "$TERM" == "linux" ]]; then
    echo -e -n "\x1b[\x32 q";
fi

它可以在 gnome-terminal 上运行,但可以在 tty 上运行。

答案1

看着https://www.kernel.org/doc/html/latest/admin-guide/vga-softcursor.html:

The cursor appearance is controlled by a ``<ESC>[?1;2;3c`` escape sequence
where 1, 2 and 3 are parameters described below. If you omit any of them,
they will default to zeroes.
first Parameter
        specifies cursor size::

                0=default
                1=invisible
                2=underline,
                ...
                8=full block
                + 16 if you want the software cursor to be applied
                + 32 if you want to always change the background color
                + 64 if you dislike having the background the same as the
                     foreground.

        Highlights are ignored for the last two flags.

second parameter
        selects character attribute bits you want to change
        (by simply XORing them with the value of this parameter). On standard
        VGA, the high four bits specify background and the low four the
        foreground. In both groups, low three bits set color (as in normal
        color codes used by the console) and the most significant one turns
        on highlight (or sometimes blinking -- it depends on the configuration
        of your VGA).

third parameter
        consists of character attribute bits you want to set.

        Bit setting takes place before bit toggling, so you can simply clear a
        bit by including it in both the set mask and the toggle mask.

Examples
--------

To get normal blinking underline, use::

        echo -e '\033[?2c'

To get blinking block, use::

        echo -e '\033[?6c'

To get red non-blinking block, use::

        echo -e '\033[?17;0;64c'

答案2

printf '\033[?112c'

( 112)0x70表示“软块光标”( 0x10) +“更改背景”( 0x20) +“前景与背景不同”( 0x40)。

这应该确保光标始终可见,无论该位置的字符单元的属性如何。

如果您不希望这样vimemacs在退出时将光标重置为默认的“闪烁下划线”,也可以执行以下操作:

infocmp linux |
sed 's/cnorm=[^,]*/cnorm=\\033[25h\\033[?112c/' |
tic -

该转义还需要另外两个参数,它们允许您修改字符单元的颜色和属性(在上面提到的 fg/bg 区分转换之前):第二个参数告诉哪些位应该是,以及第一个位应该是切换的,应用第二个首先。位的含义是VGA属性,不是curses/ANSI 颜色的颜色。例如(假设默认调色板等):

# usage: set_cursor attributes
set_cursor(){ printf '\033[?112;%d;255c' "$((~$1 & 255))"; }
           # set + toggle = clear all bits except those present in the argument

set_cursor $((0x80  | 0x8   | 0x40   | 0x6     ))
           #  hi bg | hi fg | red bg | brown fg = "yellow" fg upon "pink" bg

与其试图理解 stackexchange 的答案,不如看看源代码:add_softcursor()\e[?c解析。

相关内容