Published at
Updated at
Reading time
2min
This post is part of my Today I learned series in which I share all my web development learnings.

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.

Terminal session showing how to edit and rerun a command with "fc".

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.

If you enjoyed this article...

Join 5.7k readers and learn something new every week with Web Weekly.

Web Weekly — Your friendly Web Dev newsletter
Reply to this post and share your thoughts via good old email.
Stefan standing in the park in front of a green background

About Stefan Judis

Frontend nerd with over ten years of experience, freelance dev, "Today I Learned" blogger, conference speaker, and Open Source maintainer.

Related Topics

Related Articles