Sunday, October 7, 2007

Command Line editing: emacs or vi shortcuts

You can edit your command line using emacs style shortcuts. For example, to go to the beginning of the command line, enter Control-a via keyboard input; to go to the end of line, Control-e. If you are a emacs user like me, you feel right at home.

One time, I logged in to someone else's Linux machine, and arrived at the bash shell. To my surprise, a Control-a did not take me to the start of line. Instead, I saw:

$ ./somecommand ^A^A

It did not seem to understand my emacs-styled keyboard shortcuts.

It turns out that for the bash shell, you can use other styles of command line edits besides the emacs style. Some users prefer the vi/vim style of command line edits.

To switch, set the relevant shell option. In the above example, to switch to the emacs style, type:

$ set -o emacs

If you want to switch from the default emacs to the vi style, type:

$ set -o vi

For the vi style of command line edits, you must first press the Esc key to enter the command mode. Then, you can enter the command, e.g., the ^ key to go to the beginning of the line, or the $ key to the end of the line. To return to regular typing mode, type the letter i for insert, or a for append.

1 comment:

Anonymous said...

Very good. I had been searching for an answer like this for a while. Thank you very much.