Different bash prompt for different vi editing mode?

后端 未结 9 909
深忆病人
深忆病人 2020-12-22 16:45

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

相关标签:
9条回答
  • 2020-12-22 17:12

    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

    0 讨论(0)
  • 2020-12-22 17:13

    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)$ ...
    
    0 讨论(0)
  • 2020-12-22 17:16

    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.

    0 讨论(0)
  • 2020-12-22 17:19

    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.

    0 讨论(0)
  • 2020-12-22 17:23

    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

    0 讨论(0)
  • 2020-12-22 17:24

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

    0 讨论(0)
提交回复
热议问题