August 3, 2023

Edit and reexecute a command from the shell history with fc

We will see three methods to edit and reexecute a command: using fc (fix command), Ctrl+R and Ctrl+P.

Using fc

To edit and reexecute a command from the shell history run:

$ fc -n

-n refers to the current command line minus n.

For example:

$ 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.

Similarly:

$ 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

This time, n refers to command line on line n.

For example:

$ fc 1018

will recall the command on line 1018 in the shell history.

Using Ctrl+R

If you’d rather recall a command running a search query using search terms to retrieve a command rather than numerics, type Ctrl+R. The shell should display a prompt. Type search terms to retrieve the command. When the command appears in the prompt, type Ctrl+J to copy it to the command line. Edit then hit return.

Using Ctrl+P

If you want to reexecute a command which you’ve just recently used, you can circle back through history using Ctrl+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 Ctrl+N.)


command-line interface (cli) personal computing wiki gnu linux trisquel shell literacy office applications history

No affiliate links, no analytics, no tracking, no cookies. This work © 2016-2024 by yctct is licensed under CC BY-ND 4.0 .   about me   contact me   all entries & tags   FAQ   GPG public key

GPG fingerprint: 2E0F FB60 7FEF 11D0 FB45 4DDC E979 E52A 7036 7A88