Search⌘ K
AI Features

Reusing Previous Commands and Arguments

Explore how to reuse and manipulate previous commands and their arguments efficiently using history expansion features in the command-line interface. Understand how to rerun, modify, and extract specific parts of past commands with event and word designators to streamline your workflow and avoid repetitive typing.

The !! command

By now, we’re likely familiar with the history command, and we’ve probably used the “Up” arrow key to recall a previous command we’ve typed. Way back in the chapter Introduction, we saw the !! command, which lets us rerun the previous command. This works because of a feature called history expansion, which we can use to rerun previous commands, extract arguments, or fix typos.

Let’s review the !! command before we get into the more advanced features. Let’s use the ls -lh command to get a list of the home directory:

ls -lh

We then use the !! command to execute the command again:

!!
Terminal 1
Terminal
Loading...

The ! command

The ! symbol is an event designator. It refers to some event in our history. !! is a shortcut for the last command, but ! followed by a number refers to the specific command in our history. When we execute ...