Using the vi command editor
If you run the command:
set -o vi or
export EDITOR=viit
tells the shell that you want the ability to edit commands the way
that you normally edit text with vi; you are set up for vi command
editing. Whenever the shell prompts you for input, it is as if the
shell puts you into vi insert mode on a new line at the end
of the history file. You can type in a new command just as you normally
would.You can also press <Esc to enter a vi-like
command mode. When you enter command mode, you can use the usual cursor movement commands to move
around on the command line, or to move up and down in the history file. For example:
- Press the k key to move back to the previous line in the history file (the last command line you entered). Press the k key again, and you move to the line before that.
- Press j and you move forward in the history file.
In this way it is simple to retrieve recent commands from the history file. You can then edit them using standard vi commands. For example, you can use $ to move to the end of the line, and A to begin appending text to the end of the line. When you have edited the line to produce the command that you want to run, simply press <Enter> to run that line.
As you might expect, you can use these search commands:
/string
?stringto search
backwards and forwards through the history file. You can edit the command line with these
vi commands: - w
- Move to next word
- b
- Move to previous word
- d
- delete
- c
- change
- a
- append
- i
- insert
- u
- undo