What is the zsh builtin `r` command for?
I accidentally typed r
into the shell and got what appeared to be a repeat of the last command I ran.
man zshbuiltins
, unhelpfully, says this:
r
- Same asfc -e -
.
The documentation for fc
is almost impenetrable but I managed to tease out these quotes:
fc [ -e
ename
] [ -LI ] [ -m
match
] [
old=new
... ] [
first
[
last
] ]
the editor program ename is invoked on a file containing these history events....When editing is complete, the edited command is executed.
If ename is
-
, no editor is invoked.
This reads to me like the behavior of r
is similar to exclamation point !
, in that it replays history. Indeed, if I setopt banghist
to turn bang back on, things like r man
and ! man
both seem to replay my last call to man.
What are the similarities and differences between r
and !
? What's a hypothetical scenario in which I could use r
?
Solution 1:
You have done a good job quoting the right passages from the man page.
zsh inherited r
from ksh, as neither bash or csh know this command. I suppose the implementation is done to be as compatible as possible with these three major shells.
On the other hand the history bang mechanism !
originates from csh which can be disabled with setopt NO_BANG_HIST
.
One difference between these two mechanisms which comes to my mind is that r
and !
are parsed differently, as !
is a reserved word but r
only a (builtin) command. That means you can write e.g.
$ echo my last command was !!
which gets (depending on your settings after pressing SPACE
or ENTER
) to
$ echo my last command was man zshexpn
which isn't possible with r
unless you use command substitution $(r)
of course.
So, I thinks it all just boils down to personal preferences or habits (if you used to used ksh or csh)...