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 edi
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.
.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)
This setup matches the spacemacs cursor with dotspacemacs-colorize-cursor-according-to-state
set to t
.
set editing-mode vi
set vi-ins-mode-string \1\e[5 q\e]12;green\a\2
set vi-cmd-mode-string \1\e[1 q\e]12;orange\a\2
set show-mode-in-prompt on