2024-08-25
fc
’s Core FunctionalityThe fc
command allows you to modify and re-run previously executed commands. This is incredibly useful for correcting typos, experimenting with slightly altered commands, or simply avoiding repetitive typing. It offers many options to customize its behavior.
Basic Usage:
The simplest form of fc
is just typing fc
and pressing Enter. This will open your default editor (usually vi
or nano
) displaying your most recent command. After editing, save and close the editor, and the modified command will execute.
fc # Opens editor with 'ls -l /tmp'
Specifying Command Number:
You can directly specify the command number to edit using fc -e <editor> <number>
. Command numbers start from 1, with 1 being the most recent command, 2 the second most recent, and so on.
fc -e nano 2
Specifying a Range of Commands:
fc
allows you to edit and re-execute a range of commands.
fc -e vim 1-3
Using -l
to List Commands:
The -l
option is handy for listing recent commands without editing them. It allows you to check command history before deciding which one to modify. You can specify the number of commands to list.
fc -l 5
Using -s
for Inline Editing (Without Editor):
For minor corrections, -s
allows you to directly specify the modified command on the command line. This bypasses the editor entirely.
fc -s 'ls -la'
Using -R
to Re-execute Without Editing:
If you just want to re-execute a previous command without modification, simply use the command number with fc
.
fc 3
Customizing the Editor:
By default, fc
uses the editor defined by the EDITOR
environment variable. You can override this by specifying the editor explicitly with the -e
option. For instance, if you prefer emacs
, you would use fc -e emacs
.
fc -e emacs
Working with command strings:
You can use fc
to work with command strings directly if you know the command you wish to modify without relying on command numbers. This is useful for commands further in your history that you may not want to count backwards to find the number.
fc -e nano -1 "grep error"
fc -R -1 "install"
This example uses -1
which references the last command matching the given pattern.
These examples demonstrate the versatility of fc
. By mastering these options, you can streamline your workflow and boost your command-line efficiency.