Different bash prompt for different vi editing mode?

Viewed 21463

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:

9 Answers

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.

This is what I have in ~/.inputrc

set show-mode-in-prompt on
set vi-ins-mode-string \1\e[34;1m\2└──[ins] \1\e[0m\2
set vi-cmd-mode-string \1\e[33;1m\2└──[cmd] \1\e[0m\2

Insert mode it is colored blue.

└──[ins]

Command mode it is colored yellow.

└──[cmd]

The downside is it does not display on a tty meaning it only works on a terminal emulator only the colors.

Different Prompt and Cursor Style via .inputrc

First you should make sure that you're running a bash version higher than 4.3:

$ bash --version
GNU bash, version 4.4

Then put the following lines in your ~/.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)\1\e[5 q\2
set vi-cmd-mode-string (cmd)\1\e[1 q\2

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

Spacemacs style colored cursor

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

enter image description here

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

Related