Using fc

fc is a zsh builtin command that lets you recall, edit, and execute previous commands.

There are two forms of fc.

First Form of fc

The first form is used to let you (flexibly) select previous commands, and then either display them, or load them into an editor for modifification & execution.

The syntax of the first form is:

fc [ -e EDITOR ] [ OPTIONS ] [ old=new ... ] [ first [ last ] ]

Also, see syntax notes

This will select commands from the history list between first and last, inclusive.

first and last can either be numbers or strings. A number specifies the event (command) number from the history file (1). A string specifies the most recent command that begins with that string.

After evaluating first and last, the substitutions you specified (old=new pairs) are performed on the commands. Any occurance of old in the commands is replaced with new.

Options to the First Form

Sometimes you just want to display past commands; you don't want to edit or re-run them. With the -l option, the selected commands are simply displayed; nothing else happens. (This is the same as typing history on the command line)

With -l, fc normally prints command numbers before each command. To get rid of those numbers, add the -n option.

Many other flags are used in conjunction with -l. To display time stamps of the selected commands, use the -d option. If you'd like full time-date stamps, use the -f option instead of -d. Don't like the format of the time stamps? Try the -E flag, or the -i flag. To display the elapsed time between each command line, use the -D flag.

Using the -m flag in conjunction with -l, you can specify a pattern. (don't forget to quote the pattern!). Then, only commands matching the pattern will be displayed.

Entering the Editor

With any other arguments to the first form, you'll be taken into an editor. The editor is pre-loaded with a file that contains only the history events you specified on the command-line. (One event per line).

The key idea here is that when the file is done being edited, all commands in it will be executed upon leaving the editor.

Another handy option to the first form, whether listing OR editing, is the -r flag. It reverses the order of the selected commands.

The Second Form of fc

Syntax:

fc -ARWI [ filename ]

-R will read the history from the given file

-W will write the history to the given file

-A will append the history to the given file

Adding the -I option causes only those events that are new since the last incremental append was done.

In all of the above cases, the history file never has more than $SAVEHIST entries.