You can save a lot of time typing commands in Terminal macOS. The shell keeps a record of the commands you run, and you can access this history with a few simple keystrokes to run commands again or edit them so you don’t have to retype them.
By running the command, you can see all commands in your history. Histories
command. Type the command. Histories
and press Return, Terminal displays a list of the commands in the history list. You can see an example of this:
1 ls
2 CD..
3 ls
4 cd kirk
Each line contains the number of the command, from the first to last, and the actual command. Terminal contains all commands, regardless of whether they were successful. The history will include any erroneous, or incorrectly spelled commands.
Limit the history list
There are several ways of displaying your history list. You want to see your most-recent commands in most cases. This can be done by running the Histories
command with an argument that says how many commands you want to display.
You can take, for instance: Histories
5
tells the shell to display the commands starting with the fifth one in the history list. For the argument, you can use any number. Histories
command; if you enter a number that’s invalid (most likely, your list isn’t as long as the number you entered), Terminal will respond with fc: no such event
.
These time-saving shortcuts are great!
As we discussed in a Separate articleBy pressing the arrow keys, you can scroll up or down through your history. This is the easiest way for you to run a command that you have just executed. There are other ways to tell the shell which command to run if the command is further back.
Imagine that you have displayed your long-term history list. Part of it looks something like this:
329 locate Walden
History of 330
331 ls-l
You can re-execute the command. locate Walden
Type !329
. The exclamation mark (!
A shortcut to a command from the history list is ‘()). Terminal will execute the command with that absolute number in its history if you add a number to it after it (without any space between).
Relative numbers Another way to specify a previous command is by using a relative number, or the nth 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 back from the 200th entry, enter !-5
.
Characters: You can tell the shell to run the last command that begins with a specific string of characters. Another way to execute the same command is, for example. locate Walden
command would be to type !loc
(without any space after the exclamation mark).
After the exclamation mark, you can enter as many characters as necessary. The shell will close when there is a string with these characters. In the above example, I could have typed !lo
since there were no other commands that began with those letters. However, if I had only entered !l
The example would have executed command 331, ls
command, because this would have been the first match.
Editor’s note: Parts of this article were adapted from The Mac OS X Command Line – Unix Under the HoodKirk McElhearn (2004). Reprinted with permission by Sybex. It has been updated in order to reflect changes in Terminal for macOS Big Sur.