Rerun commands with the built-in "r" command (#tilPost)

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, y…


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

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.


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2025-02-20T23:00:00+00:00) Rerun commands with the built-in "r" command (#tilPost). Retrieved from https://www.scien.cx/2025/02/20/rerun-commands-with-the-built-in-r-command-tilpost/

MLA
" » Rerun commands with the built-in "r" command (#tilPost)." Stefan Judis | Sciencx - Thursday February 20, 2025, https://www.scien.cx/2025/02/20/rerun-commands-with-the-built-in-r-command-tilpost/
HARVARD
Stefan Judis | Sciencx Thursday February 20, 2025 » Rerun commands with the built-in "r" command (#tilPost)., viewed ,<https://www.scien.cx/2025/02/20/rerun-commands-with-the-built-in-r-command-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » Rerun commands with the built-in "r" command (#tilPost). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/02/20/rerun-commands-with-the-built-in-r-command-tilpost/
CHICAGO
" » Rerun commands with the built-in "r" command (#tilPost)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2025/02/20/rerun-commands-with-the-built-in-r-command-tilpost/
IEEE
" » Rerun commands with the built-in "r" command (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2025/02/20/rerun-commands-with-the-built-in-r-command-tilpost/. [Accessed: ]
rf:citation
» Rerun commands with the built-in "r" command (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2025/02/20/rerun-commands-with-the-built-in-r-command-tilpost/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.