How to show the current Vi mode when using Vi keybindings in the shell?

8,757

Solution 1

I don't believe there is any way to determine visually what mode your in, but if you modify your behavior a little it won't matter.

If you want to be in command mode, press ESC before typing a command.

If you want to be in insert mode, press ESC and i before typing content.

You will end up pressing ESC a lot, but every vi user I've ever met does that anyway.

Solution 2

To anyone having this exact problem (like myself): I found a solution at stackoverflow. Apparently there is native support for this in readline, so you can just add the following to your ~/.inputrc:

set show-mode-in-prompt on

And to customize the actual character use:

set vi-ins-mode-string "custom-ins"
set vi-cmd-mode-string "custom-cmd"

Solution 3

This page has a fairly detailed script which sets up what you want (and more) in zsh. Perhaps it could be adapted to work with tcsh (I am not terribly familiar with that shell).

Share:
8,757

Related videos on Youtube

Sundar R
Author by

Sundar R

#SOreadytohelp

Updated on September 17, 2022

Comments

  • Sundar R
    Sundar R over 1 year

    I use the vi keybindings in my tcsh shell.

    Now, I don't know which vi mode I'm in (insert mode or command mode), which leads to a lot of frustration.

    Does anyone know of a way to show the current vi mode in the shell? (by including in the prompt or something)

  • Sundar R
    Sundar R over 14 years
    The problem is that, in the shell, the extra ESCs are not ignored - when I give an extra ESC, then type a command (say h to move left), it simply gives a beep and doesn't do anything.
  • Sundar R
    Sundar R over 14 years
    By "extra ESC", I mean pressing ESC when I'm already in command mode - which I do a lot since I don't know what mode I'm in.
  • Bob Weber
    Bob Weber over 14 years
    You are correct. I've used it so long that I don't even notice that behaviour any longer. Again, the simplest answer. If you hit ESC and h and nothing happens, hit h again. I think that opens up another question. Why does pressing ESC while in the 'command' mode of the vi keybindings cause the next character to not be interpreted correctly? vi itself doesn't do this. I can type ESC 10 times and the next command still works as anticipated.
  • Bob Weber
    Bob Weber over 14 years
    Another tip, turn off the bell. It is incredibly annoying just for the reason you describe - and because it beeps on tab completion. There are various ways of doing this depending on your environment.
  • BrianH
    BrianH over 14 years
    Your links go to copyrighted O'Reilly books...
  • BrianH
    BrianH over 14 years
    I'm not familiar with tcsh either, but I think the page you showed uses zsh widgets (with zle). I don't think other shells have that, but if they do, that would definitely do the trick.
  • Sundar R
    Sundar R over 14 years
    Ok, I'm currently giving this solution a try. Mapping Caps Lock to Escape has made implementing this easier, but I'm yet to get used to ESC-i for insert mode. I hope I will soon. >If you hit ESC and h and nothing happens, hit h again The problem with this is, it is quite frustrating and breaks the flow a lot. It's an extra thing that I have to think about in addition to the command I'm forming. I prefer vi keybindings for the very reason that they're in my muscle memory and give one less thing to think about - this sort of breaks that purpose itself...
  • Hrishikesh Kadam
    Hrishikesh Kadam about 2 years
    In bash 5.1.8, I am getting @ symbol before my terminal $PS1 prompt.