21

I just discovered the wonders of set -o vi, and am curious if it is possible to customize this vi shell mode in some of the same ways you can customized vi or vim?

For example, create a custom key binding to map a more convenient key or key combo to esc?

If it's not currently supported, how difficult would it be to alter the source code of the program and hardcode in some customizations, or would that be a bad idea?

Jonah
  • 1,059

1 Answers1

19

Yes, you can change key mappings (for either vi- or emacs-like mode) with the bind builtin. This is actually a readline feature (so you can have vi-like bindings in all readline programs, not just bash).

The key you're looking for is called vi-movement-mode, and defaults to \e (escape). You could additionally bind it to (and this is rather silly) equals like this. The second line gets rid of the binding to escape:

$ bind "=":vi-movement-mode 
$ bind -r "\e"

Running bind -p will show you all the keys you can bind, and their current bindings (if any).

You can put bind commands in your .bashrc, or you can edit ~/.inputrc to make it apply to all readline programs you run. The readline docs contain full details.

derobert
  • 109,670
  • 1
    This is awesome. Thanks so much. I was able to figure out how to bind key combos like bind "Control-Space":vi-movement-mode -- that works fine. But for example in vim I have typing two characters in a row as trigger to esc, ie, if you type jj you'll return to normal mode. This did not work when I tried it with bind, is there a special way to make it work? – Jonah Apr 29 '13 at 19:15
  • @Jonah not that I know of off-hand, but I haven't perused the readline docs recently... – derobert Apr 29 '13 at 19:26
  • @chishaku you need to prefix Jonah's name with an at sign (@), or he won't get a notification. – derobert Nov 24 '14 at 21:31
  • 2
    @Jonah I just tried to do the same thing. Did you ever figure this out? – chishaku Nov 24 '14 at 21:41
  • 2
    https://unix.stackexchange.com/questions/303282/in-bash-vi-mode-map-jk-to-exit-insert-mode You need quotes for some reason bind '"jk":vi-movement-mode' – Hielke Walinga Aug 15 '18 at 19:25