Skip to content

A time-saving way to re-run your last terminal command

Abstract

Introducing zsh’s command r and how to re-create it in bash.

The Z shell has a built-in named r that optionally takes an argument, and re-runs/repeats

  • the last command in your history that begins with the given argument (if an argument was given).
  • the last command in your history, period (if no argument was given).

Example#

Imagine you executed these commands:

pip install --upgrade pip
echo "I just upgraded pip."

Executing r pip would execute the last command in your history that begins with pip. In our case, it would run pip install --upgrade pip again.

Running simply r (without any argument) would run the last command again. In our case, it would run echo "I just upgraded pip." again.

Comparison to equivalent commands#

History substitution with !!#

Running r without arguments is equivalent to first executing !! (look up history substitution if this is news to you) which gives you the last command in your shell history, and then pressing the Enter key to actually execute the command (unless you disabled the requirement to confirm the command by pressing Enter before you execute it).

Example

Imagine running apt update (without sudo) only to be nagged about forgetting sudo. Then you could simply run sudo !! to prepend the previous command with sudo and not have to type apt update again. If you wanted to run sudo apt update for a second time, you could now simply execute !! because now sudo apt update would be the last command in your history.

Depending on your settings, you might have to press the Enter key after entering !! for confirmation before the command is executed. With r, you don’t have to.

Perhaps you already use the zsh add-on zsh-history-substring-search with which you could simply start typing pip and then press the Up arrow on your keyboard to find the last command that contains (and not necessarily starts with) pip.

Imagine you ran these commands:

pip install --upgrade pip
python3 -m pip install --upgrade black
  • The command r pip would skip over python3 -m pip install --upgrade black and immediately execute pip install --upgrade pip (without requiring a confirmation first).
  • With zsh-history-substring-search, by entering pip into the terminal and then pressing the Up or Down arrow key, you could cycle through both of these commands (because both contain the string pip) and then confirm your choice by pressing the Enter key.

You can simultaneously press the Control key and the r key on your keyboard to open what is known as the reverse incremental search. You can then begin typing your command (for example, pip install) and then use the Up and Down arrows on your keyboard to choose the wanted command from your history, and then confirm it by pressing Enter.

This, again, takes a few more keystrokes than using the command r if you’re absolutely sure you want to execute the latest matching command without needing to confirm it (for example, because you previously executed it just three seconds ago).

How to use r with bash#

This command r exists only in zsh. If you want that command in bash too, you can recreate that command by defining the following alias:

alias r='fc -e -'

Because you don’t need that alias when you’re using zsh, you can put the alias definition inside an if-construct to activate the alias only when you’re using bash:

if [[ -n "${BASH}" ]]; then
  alias r='fc -e -'
fi

Final words#

Today I learned about the built-in r. Hopefully, you could learn something from this post too. In any case, be well.

Buy Me A Coffee

Update

I wrote a follow-up article to provide you with some bonus knowledge. 🙂

And another follow-up article that explains a bit more in-depth how it works rather than just how you use it.

Comments