Search code examples
bashterminalvireadlinexterm

Can I view in which mode I am while using vi-mode for terminal (xterm)?


The question is pretty self explanatory. I've heard from a co-worker there is a way to view in which mode I currently am when using vi-mode in terminal but somehow he forgot how it's done and I couldn't find an answer to that question. How can I do it?


Solution

  • This is a readline feature. To enable it, you have to set

    set show-mode-in-prompt on
    

    in your ~/.inputrc file. This requires readline 6.3 or newer (bundled with Bash 4.3 or newer).

    In vi mode, the mode strings then default to (ins) and (cmd):

    (cmd)$
    (ins)$
    

    This assumes that your prompt is simply PS1='\$ '.

    You can customize them with two other readline settings, for example

    set vi-cmd-mode-string [c]
    set vi-ins-mode-string [i]
    

    resulting in

    [c]$
    [i]$
    

    Notice that non-printing characters (like terminal escapes for colours) have to be escaped with \1 and \2. These two settings require readline 7.0 or newer (bundled with Bash 4.4 or newer).

    Manual entries: