Tuesday, June 16, 2009

Use VI command in bash

If you are a fan of vi editor, you'll be exciting to find out that we can use vi command in shell.
By default, bash has its own set of short cut to make it easier to type and change command. But it's quite different from what we vi users have get used to. It's a pain to try to remember two different style of short cuts.
Fortunately, bash has the ability to allow us to retain our vi habit. To turn this feature on, simply run set -o vi . Then we can verify this feature is enabled by running set -o and see if it shows vi on.
Having turned it on, we can edit our commands just as in vi. By default, the shell is in insert mode. By pressing escape, it turns to command mode in which we can use vi command to move, search around.
For example, we can find a command in history by :
  1. press escape
  2. press /
  3. input search terms
  4. press n to search for next match
To view and edit shortcut keys in bash, we can use the bind build-in command.
Bonus: In windows prompt, we can use F7 to get a command history list and F8 to search in command history.

References:
Master the Linux bash command line with these 10 shortcuts
Useful Keyboard Shortcuts for the DOS Command Prompt in Windows
Find and bind key sequences in bash

No comments: