Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different bash prompt for different vi editing mode?

Tags:

bash

prompt

People also ask

How do I set Bash to vi mode?

The bash shell (again, via GNU Readline) is able to provide this functionality for us. In order to enable it, you run the command $ set -o vi.

Which of the following is the default command line editing mode in Bash?

Fortunately, you can change the command line editing mode used by Bash. The default is emacs-mode, but you can easily change it to vi-mode.

Which of the following is the default command line editing mode in Bash vi Ed emacs Vim?

Bash provides two modes for command line editing - emacs and vi. Emacs editing mode is the default and I already wrote an article and created a cheat sheet for this mode.


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 ;)


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:

enter image description here

More details, including how to install, are available at https://github.com/calid/bash


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.


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 _

Preview(Source)


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