不同的 vi 编辑模式的不同 bash 提示?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1039713/
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
Different bash prompt for different vi editing mode?
提问by
When using vi mode (set -o vi) with Bash, it would be nice to have a prompt that depends on the mode you are currently in (insert or command). How does one find out this editing mode?
在 Bash 中使用 vi 模式 (set -o vi) 时,最好有一个取决于您当前所处模式(插入或命令)的提示。如何找到这种编辑模式?
B.t.w, this seems to be possible in ZSH:
顺便说一句,这在ZSH 中似乎是可能的:
回答by eMPee584
Fresh bash 4.3 and readline 6.3 have something for you guys.. from the changelog:
Fresh bash 4.3 和 readline 6.3 为你们提供了一些东西..来自更新日志:
4. New Features in Readline
j. New user-settable variable, show-mode-in-prompt, adds a characters to the
beginning of the prompt indicating the current editing mode.
So putting
所以放
set show-mode-in-prompt on
into /etc/inputrc or ~/.inputrc (thx stooj) should affect all your readline-enabled programs ;)
进入 /etc/inputrc 或 ~/.inputrc (thx stooj) 应该会影响所有启用 readline 的程序;)
回答by Dylan Cali
Bash 4.4 / Readline 7.0 will add support for user-settable mode strings.
Bash 4.4 / Readline 7.0将添加对用户可设置模式字符串的支持。
You can try the beta versions, but they seem a bit buggy at the moment. They also don't yet support specifying where in the prompt you want the mode indicator to occur (which I think is the killer feature).
您可以尝试测试版,但目前它们似乎有点问题。他们也不支持在提示中指定您希望模式指示器出现的位置(我认为这是杀手级功能)。
If you don't want to wait, and aren't afraid of a little compilation, I've published patched versions of bash 4.3 and readline 6.3 to github that support this functionality.
如果您不想等待,并且不怕一点点编译,我已经将支持此功能的 bash 4.3 和 readline 6.3 的修补版本发布到 github。
With the patched versions you can do stuff like this:
使用修补版本,您可以执行以下操作:
More details, including how to install, are available at https://github.com/calid/bash
更多详细信息,包括如何安装,请访问https://github.com/calid/bash
回答by Jeremy Heslop
After searching google, looking through the bash man page and then looking through the bash source code (the lib/readline/vi_mode.c) it looks like there is no easy way to change the prompt when moving from insert mode to command mode. It looks like there might be an opportunity here for someone to patch the bash source though as there are calls for starting and stopping the modes in the source.
在搜索谷歌后,查看 bash 手册页,然后查看 bash 源代码(lib/readline/vi_mode.c),似乎没有简单的方法可以在从插入模式移动到命令模式时更改提示。尽管有人要求启动和停止源中的模式,但似乎有人可以在这里修补 bash 源。
Upon seeing your post it got me interested in the bash vi mode setting. I love vi and would why not on the command line. However it looks like we will have to keep track of whether we are in insert mode without a prompt change (so sayeth many forum posts) For what it is worth you are always in insert mode unless you hit ESC. Makes it a little easier, but not always as intuitive.
看到您的帖子后,我对 bash vi 模式设置产生了兴趣。我喜欢 vi,为什么不在命令行上。然而,看起来我们将不得不在没有及时更改的情况下跟踪我们是否处于插入模式(许多论坛帖子都这么说)为了值得您始终处于插入模式,除非您按 ESC。使它更容易一些,但并不总是那么直观。
I'm upping your question as I'm interested in seeing where this goes.
我正在提出你的问题,因为我有兴趣看看这会发生什么。
回答by Jeremy Heslop
Multiline prompt and .inputrc
多行提示和 .inputrc
Inputrc has an option to show a +for insert and :for normal mode, by adding set show-mode-in-prompt onin the ~/.inputrcas eMPee584 wrote, but this does not work well with multiline prompt (with older versions of bash and readline).
INPUTRC有一个选项,以显示一个+插入和:正常模式下,通过增加set show-mode-in-prompt on在~/.inputrc作为eMPee584写道,但这并不能很好地多提示(与旧版本的bash和readline的)工作。
A solution is have a single line PS1(>), and a function that echo something before the prompt. It is built into bash and called PROMPT_COMMAND.
解决方案是有一行PS1( >) 和一个在提示前回显某些内容的函数。它内置于 bash 中并称为PROMPT_COMMAND.
function prompt {
PS1=' > '
echo -e "$(date +%R) $PWD"
}
PROMPT_COMMAND='prompt'
The usual prompt strings are not available in echo of printf. The -eis to interprete color codes, and it is not necessary to add \[or \], which doesn't work anyway.
printf 的 echo 中没有通常的提示字符串。该-e是interprete颜色代码,这是没有必要添加\[或\],不反正工作。
Insertmode:
插入模式:
20:57 /home/sshbio/dotfiles/bash
+ > _
Normalmode:
普通模式:
20:57 /home/sshbio/dotfiles/bash
: > _
Pressing tab, only the PS1 is repeated, which makes sense for me:
按 tab,只重复 PS1 ,这对我来说很有意义:
20:57 /home/sshbio/dotfiles/bash
+ > ls _
bashrc bash_profile inputrc
+ > ls _
回答by Jetchisel
This is what I have in ~/.inputrc
这就是我所拥有的 ~/.inputrc
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└──[錭md] \e[0m
Insert mode it is colored blue.
插入模式为蓝色。
└──[ins]
Command mode it is colored yellow.
命令模式它是黄色的。
└──[錭md]
The downside is it does not display on a ttymeaning it only works on a terminal emulator only the colors.
缺点是它不显示,tty它只能在终端仿真器上显示颜色。
回答by Andrew_1510
I try to get a indicator for BASH vi mode also, and you all learned it's sound simple and just no way to do it yet.
我也尝试获取 BASH vi 模式的指示器,你们都知道这听起来很简单,但目前还没有办法做到。
My current approach is: hit 'a' when I not sure which mode is. IF 'a' appears after BASH PROMOT, I learn I am in 'INSERT' mode. THEN, I hit 'RETURN' and continue. This is a easy way for me to solve the small annoyance.
我目前的方法是:当我不确定是哪种模式时点击“a”。如果 BASH PROMOT 后出现“a”,我知道我处于“插入”模式。然后,我点击“返回”并继续。这是我解决小烦恼的简单方法。
By the way, I 'alias a='cal', or something else to give the empty hit 'a' little usefulness.
顺便说一句,我'别名 a='cal',或其他东西给空命中 'a' 一点用处。
回答by mark
for Multiline prompt like this image
对于像这个图像这样的多行提示
my work arround is like this
我的工作是这样的
my bash prompt
我的 bash 提示
export PS1=" ┌錄 \[\e[32m\]\u\[\e[m\]\[\e[32m\]@\[\e[m\]\[\e[32m\]\h\[\e[m\] \w \$ \n "
.inputrc
.inputrc
set show-mode-in-prompt on
set vi-ins-mode-string " └──錄 (ins):"
set vi-cmd-mode-string " └──錄 (cmd):"
hope this helped you
希望这对你有帮助
回答by winklerrr
Different Prompt and Cursor Style via .inputrc
不同的提示和光标样式通过 .inputrc
First you should make sure that you're running a bash version higher than 4.3:
首先,您应该确保您运行的 bash 版本高于4.3:
$ bash --version
GNU bash, version 4.4
Then put the following lines in your ~/.inputrc:
然后将以下几行放入您的~/.inputrc:
#################### VIM ####################
# FOR MORE INFORMATION CHECK:
# https://wiki.archlinux.org/index.php/Readline
# TURN ON VIM (E.G. FOR READLINE)
set editing-mode vi
# SHOW THE VIM MODE IN THE PROMPT (COMMAND OR INSERT)
set show-mode-in-prompt on
# SET THE MODE STRING AND CURSOR TO INDICATE THE VIM MODE
# FOR THE NUMBER AFTER `\e[`:
# 0: blinking block
# 1: blinking block (default)
# 2: steady block
# 3: blinking underline
# 4: steady underline
# 5: blinking bar (xterm)
# 6: steady bar (xterm)
set vi-ins-mode-string (ins)\e[5 q
set vi-cmd-mode-string (cmd)\e[1 q
In command mode, the cursor is displayed as block.
In insert mode, the cursor is displayed as vertical bar.
在命令模式下,光标显示为块。
在插入模式下,光标显示为竖线。
The prompt itself will then look like this depending on the mode:
根据模式,提示本身将如下所示:
(cmd)$ ...
(ins)$ ...


