newline

Table of Contents

  1. First form: fc to interactively edit a range of previous commands
  2. Second form: fc -s to replace text in a previous command and re-run it

References

TIL: the fc builtin

TIL, Shell

August 01, 2019

This my first post with the “TIL” prefix, meaning “today, I learned”. I’ll use it to designate posts that aren’t full-blown guides, but rather small things I found out (and hence these posts might be less self-contained).

The fc command is a shell builtin that lets you work with commands that you previously typed in, and its name stands for “fix command”. It has two forms: in the first, you can interactively edit a range of commands, while in the second, you can re-run a previous command after doing a replacement.

First form: fc to interactively edit a range of previous commands

This lets you take a range of previous commands and open them in an editor. Then, when you save the file and quit the editor, whatever text you edited is executed.

Usage: fc first_command_number last_command_number. This will open all commands from first_command_number up to and including last_command_number in an editor. If last_command_number isn’t specified, the current command is used. The command numbers can be relative, such as ‘-2’ for ‘two commands ago’.

Flags:

Second form: fc -s to replace text in a previous command and re-run it

In this form, you can do a global substitution (text replacement) across a previous command and re-execute it.

Usage: fc -s pattern=replacement command_number. The command number is the same as in the first form. If it’s not specified, the previous command is used.

In ZSH, this second form is a bit different. To get the same behavior as in Bash, use fc -e - pattern=replacement. You don’t need the -s flag, and -e - tells ZSH to skip the editor.