Skip to content

Instantly share code, notes, and snippets.

@ChristopherA
Last active July 17, 2023 06:43
Show Gist options
  • Save ChristopherA/5069a9c149238e60fe4f07f2ed825a84 to your computer and use it in GitHub Desktop.
Save ChristopherA/5069a9c149238e60fe4f07f2ed825a84 to your computer and use it in GitHub Desktop.
ZSH Tips - `fc` Examples

ZSH Tips - fc Examples

Tags: zsh, fc command, command history

This gist provides useful examples and tips on how to utilize the fc command in zsh (Z Shell) to interact with and manipulate your command history effectively.

  • Editing the Previous Command: Open the last command in your default editor to make changes before re-execution.

    fc
  • Repeating a Specific Command: Execute a specific command from the history without opening an editor.

    fc -s 15
  • Deleting a Range of Commands: Delete a range of commands from the history, such as deleting commands 10 to 20.

    fc -e : -l 10 -r 20 -d
  • Repeating the Previous Command with sudo: Execute the previous command with sudo privileges.

    fc -s sudo !!
  • Editing a Range of Commands: Open a specific range of commands in an editor for modification.

    fc -e <editor> -l 5 -r 10
  • Executing the Previous Command with Additional Arguments: Repeat the last command but with some modifications.

    fc -s "$(fc -ln -1 | sed 's/original/replacement/')"
  • Executing the nth-to-last Command: Execute the command that is, for example, three commands before the last command.

    fc -s -3
  • Deleting the Last Command: Delete the last command from your history.

    fc -e : -1

    This command deletes the last command from the command history using the fc command. The : represents the null editor, and -1 specifies the last command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment