I guess when you have GNU developing both the bash shell and the text editor emacs, you’re going to have one set of keyboard shortcuts work in the other. Case in point: the emacs keyboard shortcuts are also bound in the bash shell.
Well, being a vim enthusiast, that just isn’t going to work. So, here’s something that I learned at my Ubuntu-Utah LoCo meeting that ended just 20 minutes ago:
1 | aaron@hercules:~$ set -o vi |
Oh yeah. That’s better. When executed, you are in insert mode, and not command mode. So, to take advantage of the vim keyboard shortcuts, ‘esc’ or ‘ctrl-[‘ to get into command mode, and hack away! Make sure to add this to your ~/.bashrc.
You’re welcome.

{ 13 } Comments
This is amazingly cool. It might actually get me a bit better at using the vi methods of moving around. Thanks for the tip.
Even better, you can add ‘set keymap vi-insert’ to .inputrc and all of your readline using applications will have a vi keymap.
I was reluctant to switch to these because I have grown accustomed to some of the other emacs-based shortcut keys but after just a few minutes I got right back into it. Long live vim.
The best bit is pressing v in command mode which starts a proper vi windows to edit the current command. This is SO good when doing a quick script.
It would be nice if the prompt could reflect whether one is in command mode or insert mode though.
Rick- No problem!
Byron- Nice! Noted!
Christer- You won’t miss the emacs shortcuts. Except for maybe ^l.
Mathias- I agree. That should be default, as it’s part of the vi application.
Be careful, though, since Bash’s vi emulation is not 100% compatible with the vi emulation in Korn shell. For example, typing _ should insert the last word from the previous command line. Bash does this right in that it uses the last “word” including any quoted spaces, while Ksh does not — but Bash does not insert a space before the inserted word like Ksh does, and I think the extra space makes more sense.
Well, c’est la vie! (Did I spell that right?)
Oops! That previous comment should have <ESC> before the “_” (underscore) character. The HTML parser ate it.
If you miss the ^L:
bind -m vi-insert “\C-l”:clear-screen
(found on http://www.jukie.net/~bart/blog/20040326082602)
Actually, there’s a better way. If you like vi keybindings then you’ll probably want them for all your readline programs, and not just bash.
Put this in your ~/.inputrc:
set editing-mode vi
Then EVERY readline program will use vi keybindings by default. I thought everyone did this, I’ve been doing it this way for years…
So… I want to test this out but not move permanently to it…
How do I shut it off?
man set (no entry)
re-running set -o vi doesn’t do it.
set -h / –help don’t provide any info.
and there’s no -O flag.
None of the usual ideas for figuring it out worked.
Help?
Jason- As stated in the post, bash uses the emacs keybindings. So, just:
to get back to normal, or just ‘exit’ the terminal.
Are notes from that meeting going to be posted anywhere?
IMHO zsh has a better support for vi keybindings, not to say it’s waaay cooler than bash (globbing and autocompletion for example)
Post a Comment