Ben Chuanlong Du's Blog

It is never too late to learn.

Fix Shell Commands Using fc

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

fzf.history is a better alternative to fc's core functionality (edit and re-execute command).

Tips & Traps

  1. In Linux shells like Bash and Zsh, fc is a built-in command that stands for "Fix Command". Its primary purpose is to let you edit and re-execute commands from your history using $EDITOR.

  2. fc -l 1 might throw the error -bash: fc: history specification out of range in bash (some people say that this won't be an issue in zsh as zsh is smart enough to handle specification out of range but I haven't verified it yet) if the (absolute) first bash history command has been pruned (due to large number of history commands). HISTTIMEFORMAT="" history | sed -E 's/^[ ]*[0-9]+[ ]*//' is a more robust command for the same purpose.

Command Action
fc Edit the last shell command using $EDITOR and send it for execution.
fc 123 Edit and execute command number 123 from history.
fc git Edit and execute the last command starting with "git".
fc -l List all history commands.
fc -ln List all history commands without numbers.
fc -ln 100 110 List commands from 100 to 110 without numbers.
fc -ln -5 List the last 5 historical commands without numbers.
fc -s Re-execute the last command without editing.
fc -s old=new Re-execute the last command, replacing old with new.
fc -s old=new git Re-execute the last git command, replacing old with new.

Comments