Rerun commands with the built-in "r" command
- Published at
- Updated at
- Reading time
- 2min
I just learned that executing r
in my terminal, reruns the previous command. #wat
r
is a built-in shell command in my zsh setup.
$ which r
r: shell built-in command
If you check all the built-in zsh commands with man zshbuiltins
, you'll receive the unhelpful information that r
command is the same as fc -e -
.
r — Same as fc -e -
Alright, I'll bite, what's fc
?
fc
is included in the zsh built-ins, too. The fc
command lets you display, edit or execute commands from the shell history, but unfortunately, the zsh man pages fail to answer what fc
actually stands for.
Some folks say fc
stands for "fix command" whereas others advocate for "find command". Both definitions make sense, but I didn't see it written in the zsh / bash man pages.
fc
is handy when juggling long and complicated shell magic that requires editing and rerunning.
fc
isn't the only way to tackle and edit long history commands. Modern shells also supports short cuts offering this command edit functionality. But I must say, they didn't stick and fc
seems more likely to enter my muscle memory. We'll see...
But what's up with this r
command standing for fc -e -
?
The quick answer: r
uses fc
to rerun the previously executed command without the option to change it.
$ echo "hello world"
hello world
$ r
echo "hello world"
hello world
You can also narrow and search the command to run by calling r
with additional arguments. For example, r ls
will rerun the previously run ls
command.
$ ls -lah
...
$ # some more commands
$ # some more commands
$ # some more commands
$ r ls
ls -lah
...
But where is this execution behavior documented? Honestly, I've no clue...
According to the fc
documentation, the -e
flag allows you to define the used editor before rerunning the command, but -e
seems to unlock some superpowers when paired with -
. Unfortunately, I couldn't find this behavior described anywhere.
However, r
is a very convenient way to rerun a command without reaching for the arrow keys!
If you have more insights, please let me know.
Join 5.7k readers and learn something new every week with Web Weekly.