# fc — Fix Command
POSIX shell builtin for working with command history. The name comes from its original purpose: open a previous command in an editor to fix it, then re-execute.
## Cancelling
To abort without running anything, exit the editor with a non-zero status. In vim: `:cq`. Alternatively, clear the buffer and save (empty file = nothing to run).
## Editor
`fc` checks these in order: `$FCEDIT` → `$EDITOR` → default (`vi`).
To use VS Code without changing `$EDITOR`:
```zsh
export FCEDIT='code -w'
```
## Usage
### Edit and re-execute
```bash
fc # edit last command in editor, run on save
fc -3 # edit the 3rd-to-last command
fc -e vim # use vim specifically
```
### Edit a range of commands
Opens multiple commands in a single editor buffer. On save, all of them run sequentially.
Given this history:
```text
97 foo
98 bar
99 baz
```
```bash
fc 97 99 # edit foo, bar, baz — run all on save
fc 97 98 # edit foo, bar
fc -3 -1 # same as fc 97 99 (relative: 3 ago through last)
fc -3 -2 # same as fc 97 98 (3 ago through 2 ago)
```
### List history
Use `fc -l` instead of `history` to find command indices — `fc -l` doesn't add itself to history, so the numbers stay stable for subsequent `fc` calls.
```bash
fc -l # list last 16 commands
fc -l -20 # list last 20 commands
fc -l 100 110 # list commands 100-110
fc -ln # list without line numbers
```
### Re-execute
```bash
fc -s # re-execute last command
fc -s old=new # re-execute last command, replacing "old" with "new"
fc -s cd # re-execute most recent command starting with "cd"
```
### History file management (zsh)
```bash
fc -R # read history file into memory
fc -W # write in-memory history to file
fc -A # append in-memory history to file
fc -p # push current history, start fresh
```
These are useful when you edit `~/.zsh_history` directly and need to sync the running shell.
---
See also: [[Bash and Zsh]]