Edit and reexecute a command from the shell history
We will see three methods to edit and reexecute a command: using
fc (fix command),
To edit and reexecute a command from the shell history run:
$ fc -n
-n refers to the current command line minus n.
$ fc -1
will recall the last command ran in the text editor. Edit the command. Close the text editor. The edited command will be run as you quit the text editor.
$ fc -2
will recall the penultimate command you ran. And so on.
If you don’t remember the -n of the command you wish to recall, running:
$ fc -l
will print a numbered list of the last commands ran in the shell. Pick your number and run:
$ fc n
n refers to command line on line n.
$ fc 1018
will recall the command on line 1018 in the shell history.
If you’d rather recall a command running a search query using search terms to retrieve a command rather than numerics, type
R. The shell should display a prompt. Type search terms to retrieve the command. When the command appears in the prompt, type
J to copy it to the command line. Edit then hit
If you want to reexecute a command which you’ve just recently used, you can circle back through history using
P, starting from the last command ran. When the command you want to reexecute appears on the prompt, edit, then press
return. (If you passed the command you are looking for while circling back, use