简体   繁体   English

Bash的Vi命令行编辑中区分命令模式和插入模式的方法

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

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). 当vi模式下的bash切换到插入模式时,我总是有点困惑,因为它没有提供有关使用模式(命令或编辑)的任何提示。 Is there any way to distinguish mods? 有什么办法可以区分MOD吗? May be automatic change of cursor color or something like that? 可能是自动更改光标颜色或类似的东西?

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. 编辑:在最新版本的bash 4.4中,默认情况下,您将得到一个前缀为“(ins)”或“(cmd)”的提示。 but, you can change that: 但是,您可以更改以下内容:

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 "\1\e[1;31m\2:\1\e[0m\2"

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的答案的基础上,您可以通过在.inputrc进行设置来设置光标样式以反映模式(就像在VIM中一样):

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

# 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). 您的终端必须支持DECSCURSR(例如xterm,urxvt,iTerm2)。 TMUX also supports these (if you set TERM=xterm-256color outside tmux). TMUX也支持这些(如果在tmux外部设置TERM=xterm-256color )。

After years of using vi mode in korn shell, I have basically trained myself to just tap ESC a few times before I type any commands, and ESC then i to 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 ,那么您将确切知道所处的模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM