bash Bash的vi命令行编辑中区分command-mode和insert-mode的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7888387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:59:49  来源:igfitidea点击:

The way to distinguish command-mode and insert-mode in Bash's Vi command line editing

bashvicommand-line-interface

提问by chuwy

I'm always little bit confused when bash in vi-mode is switched to insert-mode, because it doesn't give any tip about used mode (command or edit). Is there any way to distinguish mods? May be automatic change of cursor color or something like that?

当 vi 模式下的 bash 切换到插入模式时,我总是有点困惑,因为它没有给出有关使用模式(命令或编辑)的任何提示。有什么方法可以区分mod吗?可能会自动更改光标颜色或类似的东西?

采纳答案by Isaac Hanson

in /etc/inputrc (or ~/.inputrc) add this:

在 /etc/inputrc (或 ~/.inputrc)中添加:

set show-mode-in-prompt on

this will prefix your prompt with +while in insert-mode, and :while in command mode in bash 4.3

这将您的前缀与提示+,而在插入模式,和在命令模式,而在bash 4.3

EDIT:in the latest version of bash 4.4, you will instead get a prompt prefixed with "(ins)" or "(cmd)" by default. but, you can change that:

编辑:在最新版本的 bash 4.4 中,默认情况下您会得到一个以“(ins)”或“(cmd)”为前缀的提示。但是,你可以改变它:

set vi-ins-mode-string "+"
set vi-cmd-mode-string ":"

also, you can use color codes like '\e[1;31m', but surround them with '\1' and '\2' to keep readline happy:

此外,您可以使用像 '\e[1;31m' 这样的颜色代码,但用 '\1' 和 '\2' 将它们包围起来,以保持 readline 愉快:

set vi-cmd-mode-string "\e[1;31m:\e[0m"

回答by laktak

Building on @Isaac Hanson's answer you can set the cursor style to reflect the mode (just like in VIM) by setting these in your .inputrc:

以@Isaac Hanson 的回答为基础,您可以通过在以下内容中设置光标样式来反映模式(就像在 VIM 中一样).inputrc

set editing-mode vi
set show-mode-in-prompt on
set vi-ins-mode-string \e[6 q
set vi-cmd-mode-string \e[2 q

# optionally:
# switch to block cursor before executing a command
set keymap vi-insert
RETURN: "\e\n"

This will give you a beam cursor in insert mode or a block cursor for normal mode.

这将为您提供插入模式下的光束光标或正常模式下的块光标。

Other options (replace the number after \e[):

其他选项(替换 后的数字\e[):

        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).

Your terminal must support DECSCURSR (like xterm, urxvt, iTerm2). TMUX also supports these (if you set TERM=xterm-256coloroutside tmux).

您的终端必须支持 DECSCURSR(如 xterm、urxvt、iTerm2)。TMUX 也支持这些(如果你TERM=xterm-256color在 tmux 之外设置)。

回答by Clarkey

After years of using vi mode in korn shell, I have basically trained myself to just tap ESCa few times before I type any commands, and ESCthen ito start typing.

在 korn shell 中使用 vi 模式多年后,我基本上已经训练自己ESC在键入任何命令之前轻敲几次,ESC然后i开始键入。

The basic premise being that if you just hit ESC, you know precisely what mode you are in.

基本前提是,如果您只是点击ESC,您就可以准确地知道自己处于什么模式。