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?
B.t.w, this seems to be possible in ZSH:
Fresh bash 4.3 and readline 6.3 have something for you guys.. from the changelog:
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 ;)
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.
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.
I'm upping your question as I'm interested in seeing where this goes.
Bash 4.4 / Readline 7.0 will add support for user-settable mode strings.
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.
With the patched versions you can do stuff like this:
More details, including how to install, are available at https://github.com/calid/bash
Multiline prompt and .inputrc
Inputrc has an option to show a + for insert and : for normal mode, by adding set show-mode-in-prompt on in the ~/.inputrc as eMPee584 wrote, but this does not work well with multiline prompt (with older versions of bash and 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.
function prompt {
PS1=' > '
echo -e "$(date +%R) $PWD"
}
PROMPT_COMMAND='prompt'
The usual prompt strings are not available in echo of printf. The -e is to interprete color codes, and it is not necessary to add \[ or \], which doesn't work anyway.
Insert mode:
20:57 /home/sshbio/dotfiles/bash
+ > _
Normal mode:
20:57 /home/sshbio/dotfiles/bash
: > _
Pressing tab, only the PS1 is repeated, which makes sense for me:
20:57 /home/sshbio/dotfiles/bash
+ > ls _
bashrc bash_profile inputrc
+ > ls _
(Source)
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.
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.
By the way, I 'alias a='cal', or something else to give the empty hit 'a' little usefulness.
for Multiline prompt like this image
my work arround is like this
my bash prompt
export PS1=" ┌錄 \[\e[32m\]\u\[\e[m\]\[\e[32m\]@\[\e[m\]\[\e[32m\]\h\[\e[m\] \w \\$ \n "
.inputrc
set show-mode-in-prompt on
set vi-ins-mode-string " └──錄 (ins):"
set vi-cmd-mode-string " └──錄 (cmd):"
hope this helped you
来源:https://stackoverflow.com/questions/1039713/different-bash-prompt-for-different-vi-editing-mode
