如何根据 Bash 中的 VI 模式更改光标形状?
How to change cursor shape depending on VI mode in Bash?
我的 .bashrc 中有以下行:
set -o vi
而且我希望我的光标在插入模式下为管道形状,在命令模式下为块状,就像我在 Vim 中放置以下内容时的情况一样.vimrc:
let &t_SI = "\e[6 q"
let &t_SR = "\e[4 q"
let &t_EI = "\e[2 q"
除了在这种情况下,我希望在命令行上具有等效的行为。
我在这里找到了我的问题的部分答案 -
https://unix.stackexchange.com/questions/22527/change-cursor-shape-or-color-to-indicate-vi-mode-in-bash - 由@gogolb 撰写。
这是答案,已复制:
#!/bin/bash
# Script "kmtest.sh"
TEST=`bind -v | awk '/keymap/ {print $NF}'`
if [ "$TEST" = 'vi-insert' ]; then
echo -ne "3]12;Green[=13=]7"
else
echo -ne "3]12;Red[=13=]7"
fi
export PS1="\u@\h $(kmtest.sh)> "
不幸的是,正如答案中所解释的那样,示例脚本仅在回车后更改光标形状 return,而我想要的是在我按下 时更改光标形状(即当我改变模式时)。
我正在使用 Linux 运行 本机终端应用程序,Bash 4.4.7 并且我的 $TERM 变量设置为 xterm-256color。另外,我不知道 tmux 是否对我的要求有任何影响,但理想情况下,我希望该解决方案在 tmux 会话内外都能正常工作。
解决方案
我自己找到了这个问题的答案,我在我发布的另一个问题中对此进行了描述:
How to correctly link patched GNU readline library to all existing programs?
别担心,该解决方案不需要任何补丁。 ;)
解决方案:
我将按照建议在此处发布我自己的问题的答案。
此解决方案适用于 Bash 4.4+,因为从该版本的 Bash 开始,使用了 GNU readline 库的 7.0 版,其中包括必要的 vi-cmd-mode-string
和 vi-ins-mode-string
个变量。
这些变量可以在你的 .inputrc 文件中设置如下,以实现我上面描述的功能:
set show-mode-in-prompt on
set vi-cmd-mode-string "\e[2 q"
set vi-ins-mode-string "\e[6 q"
解释:
对于那些真正对上述解决方案的工作原理感兴趣的人。
这两个变量 vi-cmd-mode-string
和 vi-ins-mode-string
与命令提示符一起打印到您的终端,以便提供一种关于您当前所处模式的视觉指示器(即命令模式与插入模式)。
对于命令和插入模式,这两个变量的默认值分别为“(cmd)”和“(ins)” .因此,如果您只是将它们保留为默认值并有一个命令提示符,比如说 PS1='>>>'
,那么您的提示符将如下所示:
命令模式:
(cmd) >>>
插入模式:
(ins) >>>
根据 readline 的手册页(见下文),您还可以通过在 \ 之间嵌入序列来指定不可打印的字符,例如终端控制序列1 和 \2 转义字符。
vi-cmd-mode-string ((cmd))
This string is displayed immediately before the last line of the primary prompt when vi editing mode is active and in command mode. The value is expanded like a key binding, so the
standard set of meta- and control prefixes and backslash escape sequences is available. Use the and escapes to begin and end sequences of non-printing characters, which can be
used to embed a terminal control sequence into the mode string.
vi-ins-mode-string ((ins))
This string is displayed immediately before the last line of the primary prompt when vi editing mode is active and in insertion mode. The value is expanded like a key binding, so the
standard set of meta- and control prefixes and backslash escape sequences is available. Use the and escapes to begin and end sequences of non-printing characters, which can be
used to embed a terminal control sequence into the mode string.
因此,在我上面的解决方案中,我在这些 \ 1 和 \2 转义字符,导致我的光标在命令模式下呈竖线形状,在插入模式下呈管道形状。
这太棒了。我想补充一点,除了调整光标外,还可以有文本模式状态消息。此代码有效:
set show-mode-in-prompt on
set vi-cmd-mode-string "\e[2 qcmd"
set vi-ins-mode-string "\e[6 qins"
cmd
和 ins
将根据模式显示在提示的左侧。
如果您想在其他程序之前将光标重置为正常 运行,则可以使用环境变量 PS0
。来自 man bash
:
The value of this parameter is expanded (see PROMPING below) and displayed by interactive shells after reading a command and before the command is executed.
使用此命令设置 PS0 将导致它将光标恢复为非闪烁块:
PS0="\e[2 q"
这就是我正在使用的,是的 unicode。唯一的缺点是,当您不是 运行 X 服务器时,unicode 很无聊! :-)
set show-mode-in-prompt on
set vi-ins-mode-string \e[34;1m└──[ins] \e[0m
set vi-cmd-mode-string \e[33;1m└──[ cmd] \e[0m
Alan Barnett 的回答:
Setting PS0 with this command will cause it to restore the cursor to a non-blinking block:
PS0="\e[2 q"
解决了我的 Vim 问题,但我写的不是 PS0="\e[2 q"
,而是 PS0="\e[2 q"
。艾伦的回答以某种方式在我的任何命令的终端输出中添加了一个 ]
。
我的 .bashrc 中有以下行:
set -o vi
而且我希望我的光标在插入模式下为管道形状,在命令模式下为块状,就像我在 Vim 中放置以下内容时的情况一样.vimrc:
let &t_SI = "\e[6 q"
let &t_SR = "\e[4 q"
let &t_EI = "\e[2 q"
除了在这种情况下,我希望在命令行上具有等效的行为。
我在这里找到了我的问题的部分答案 - https://unix.stackexchange.com/questions/22527/change-cursor-shape-or-color-to-indicate-vi-mode-in-bash - 由@gogolb 撰写。
这是答案,已复制:
#!/bin/bash
# Script "kmtest.sh"
TEST=`bind -v | awk '/keymap/ {print $NF}'`
if [ "$TEST" = 'vi-insert' ]; then
echo -ne "3]12;Green[=13=]7"
else
echo -ne "3]12;Red[=13=]7"
fi
export PS1="\u@\h $(kmtest.sh)> "
不幸的是,正如答案中所解释的那样,示例脚本仅在回车后更改光标形状 return,而我想要的是在我按下
我正在使用 Linux 运行 本机终端应用程序,Bash 4.4.7 并且我的 $TERM 变量设置为 xterm-256color。另外,我不知道 tmux 是否对我的要求有任何影响,但理想情况下,我希望该解决方案在 tmux 会话内外都能正常工作。
解决方案
我自己找到了这个问题的答案,我在我发布的另一个问题中对此进行了描述:
How to correctly link patched GNU readline library to all existing programs?
别担心,该解决方案不需要任何补丁。 ;)
解决方案:
我将按照建议在此处发布我自己的问题的答案。
此解决方案适用于 Bash 4.4+,因为从该版本的 Bash 开始,使用了 GNU readline 库的 7.0 版,其中包括必要的 vi-cmd-mode-string
和 vi-ins-mode-string
个变量。
这些变量可以在你的 .inputrc 文件中设置如下,以实现我上面描述的功能:
set show-mode-in-prompt on
set vi-cmd-mode-string "\e[2 q"
set vi-ins-mode-string "\e[6 q"
解释:
对于那些真正对上述解决方案的工作原理感兴趣的人。
这两个变量 vi-cmd-mode-string
和 vi-ins-mode-string
与命令提示符一起打印到您的终端,以便提供一种关于您当前所处模式的视觉指示器(即命令模式与插入模式)。
对于命令和插入模式,这两个变量的默认值分别为“(cmd)”和“(ins)” .因此,如果您只是将它们保留为默认值并有一个命令提示符,比如说 PS1='>>>'
,那么您的提示符将如下所示:
命令模式:
(cmd) >>>
插入模式:
(ins) >>>
根据 readline 的手册页(见下文),您还可以通过在 \ 之间嵌入序列来指定不可打印的字符,例如终端控制序列1 和 \2 转义字符。
vi-cmd-mode-string ((cmd))
This string is displayed immediately before the last line of the primary prompt when vi editing mode is active and in command mode. The value is expanded like a key binding, so the
standard set of meta- and control prefixes and backslash escape sequences is available. Use the and escapes to begin and end sequences of non-printing characters, which can be
used to embed a terminal control sequence into the mode string.
vi-ins-mode-string ((ins))
This string is displayed immediately before the last line of the primary prompt when vi editing mode is active and in insertion mode. The value is expanded like a key binding, so the
standard set of meta- and control prefixes and backslash escape sequences is available. Use the and escapes to begin and end sequences of non-printing characters, which can be
used to embed a terminal control sequence into the mode string.
因此,在我上面的解决方案中,我在这些 \ 1 和 \2 转义字符,导致我的光标在命令模式下呈竖线形状,在插入模式下呈管道形状。
这太棒了。我想补充一点,除了调整光标外,还可以有文本模式状态消息。此代码有效:
set show-mode-in-prompt on
set vi-cmd-mode-string "\e[2 qcmd"
set vi-ins-mode-string "\e[6 qins"
cmd
和 ins
将根据模式显示在提示的左侧。
如果您想在其他程序之前将光标重置为正常 运行,则可以使用环境变量 PS0
。来自 man bash
:
The value of this parameter is expanded (see PROMPING below) and displayed by interactive shells after reading a command and before the command is executed.
使用此命令设置 PS0 将导致它将光标恢复为非闪烁块:
PS0="\e[2 q"
这就是我正在使用的,是的 unicode。唯一的缺点是,当您不是 运行 X 服务器时,unicode 很无聊! :-)
set show-mode-in-prompt on
set vi-ins-mode-string \e[34;1m└──[ins] \e[0m
set vi-cmd-mode-string \e[33;1m└──[ cmd] \e[0m
Alan Barnett 的回答:
Setting PS0 with this command will cause it to restore the cursor to a non-blinking block:
PS0="\e[2 q"
解决了我的 Vim 问题,但我写的不是 PS0="\e[2 q"
,而是 PS0="\e[2 q"
。艾伦的回答以某种方式在我的任何命令的终端输出中添加了一个 ]
。