With the Terminal in macOS, you can save a lot of time and type less by using the command history functions built into the Terminal shell. The shell keeps track of the commands you run and you can access this history with a few simple keystrokes to rerun or edit commands so you don’t have to retype them.
You can check which commands are in your history at any time by checking the history order. When you type history and hit return, Terminal displays a list of the commands in the history list. For example:
2 cd ..
4 cd kirk
Each line contains the number of the command (from the first to the last) and the command itself. Terminal contains all commands whether they were successful or not. This means that misspelled or misspelled words will appear in history.
Narrow the history list
There are several ways to view your history list. In most cases, you want to see the most recent commands. One way to do this is by using the history command with an argument that says how many commands you want to display.
For example, history5 tells the shell to list the commands starting with the fifth in the history list. You can enter any number as the argument for the history order; if you enter an invalid number (probably your list is not as long as the number you entered), Terminal responds with fc: no such event.
As discussed in a separate article, you can move up or down in your history list by pressing the arrow keys. This is the easiest way to rerun a command you recently ran. But if your command is further back in the list, there are faster ways to tell the shell which one to run.
Let’s say you’ve displayed your long history list, and part of it looks like this:
329 locate Walden
331 ls -l
If you want to run the command again: locate Waldentype !329. The exclamation mark (!) is a shortcut for a command in the history list. If you enter a number after it (with no space in between), Terminal will run the command with that absolute number in its history.
Relative numbers: Another way to specify a previous command is to use a . to use family member number, or the this much command back from the end of the list. For example, if your history is 200 entries long and you want to enter the fifth command from the 200th entry, type: !-5.
Characters: You can tell the shell to run the last command that starts with a specific string of characters. For example, another way to do the same locate Walden command would be to type !loc (without a space after the exclamation mark).
Enter as few characters as you want after the exclamation mark. The shell stops on the first occurrence of a string that matches these characters. In the example just mentioned, I could have typed !lo because there were no other commands that started with those letters. But if I had just come in !lthe example would have executed command 331, the ls -l command, as this would have been the first match.
Editor’s Note: Portions of this article are adapted fromThe Mac OS X Command Line: Unix Under the Hood, by Kirk McElhearn (2004; reprinted with permission from Sybex). It has been updated to reflect the changes in the Terminal for macOS Big Sur.