25

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 as fc -e -.

The documentation for fc is almost impenetrable but I managed to tease out these quotes:

  • fc [ -eename] [ -LI ] [ -mmatch] [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?

1 Answer 1

20

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)...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .