Emacs and Vi modes in Bash
Two options that can be set using the set command that will be of some interest to the common user are "-o vi" and "-o emacs". As with all of the environment modifying commands these can be typed at the command prompt or inserted into the appropriate file mentioned above.
- $ set -o emacs
- This is usually the default editing mode when in the bash environment and means that you are able to use commands like those in Emacs (defined in the Readline library) to move the cursor, cut and paste text, or undo editing.
- Commands to take advantage of bash's Emacs Mode:
- o ctrl-a Move cursor to beginning of line
- o ctrl-e Move cursor to end of line
- o meta-b Move cursor back one word
- o meta-f Move cursor forward one word
- o ctrl-w Cut the last word
- o ctrl-u Cut everything before the cursor
- o ctrl-k Cut everything after the cursor
- o ctrl-y Paste the last thing to be cut
- o ctrl-_ Undo
- o NOTE: ctrl- = hold control, meta- = hold meta (where meta is usually the alt or escape key).
- o A combination of ctrl-u to cut the line combined with ctrl-y can be very helpful. If you are in middle of typing a command and need to return to the prompt to retrieve more information you can use ctrl-u to save what you have typed in and after you retrieve the needed information ctrl-y will recover what was cut.
- $ set -o vi
- Vi mode allows for the use of vi like commands when at the bash prompt. When set to this mode initially you will be in insert mode (be able to type at the prompt unlike when you enter vi). Hitting the escape key takes you into command mode.
- Commands to take advantage of bash's Vi Mode:
- o h Move cursor left
- o l Move cursor right
- o A Move cursor to end of line and put in insert mode
- o 0 (zero) Move cursor to beginning of line (doesn't put in insert mode)
- o i Put into insert mode at current position
- o a Put into insert mode after current position
- o dd Delete line (saved for pasting)
- o D Delete text after current cursor position (saved for pasting)
- o p Paste text that was deleted
- o j Move up through history commands
- o k Move down through history commands
- o u Undo