
How to have a look at your command historical past listing in macOS’s Terminal
With the Terminal in macOS, it can save you a lot of time and kind much less by utilizing the command historical past capabilities constructed into the Terminal shell. The shell retains a report of the instructions you run, and you may entry this historical past with a number of easy keystrokes to run instructions once more or edit them so that you don’t must retype them.
You can test to see what instructions are in your historical past at any time by operating the historical past
command. When you kind historical past
and press Return, Terminal shows an inventory of the instructions within the historical past listing. For instance:
1 ls
2 cd ..
3 ls
4 cd kirk
Each line contains the variety of the command (from the primary to the final) and the command itself. Terminal contains all instructions, whether or not or not they had been profitable. This implies that misguided or misspelled ones will seem within the historical past.
Limit the historical past listing
There are a number of methods of displaying your historical past listing. In most instances, you need to see the most-recent instructions. One means to do that is to run the historical past
command with an argument that claims what number of instructions you need to show.
For instance, historical past
5
tells the shell to show the instructions beginning with the fifth one within the historical past listing. You can enter any quantity as an argument for the historical past
command; in case you enter a quantity that’s invalid (almost definitely, your listing isn’t so long as the quantity you entered), Terminal will reply with fc: no such occasion
.
Time-saving shortcuts
As mentioned in a separate article, you possibly can transfer up or down your historical past listing by urgent the arrow keys. This is the simplest option to rerun a command you executed lately. But in case your command is additional again within the listing, there are faster methods to inform the shell which one to run.
Say you might have displayed your lengthy historical past listing, and a part of it seems like this:
329 find Walden
330 historical past
331 ls -l
If you need to re-execute the command find Walden
, kind !329
. The exclamation level (!
) is a shortcut for a command within the historical past listing. If you enter a quantity after it (with no house between), Terminal runs the command that has that absolute quantity within the historical past.
Relative numbers: Another option to specify a earlier command is by utilizing a relative quantity, or the nth command again from the top of the listing. For instance, in case your historical past is 200 entries lengthy and also you need to enter the fifth command again from the 2 hundredth entry, enter !-5
.
Characters: You can inform the shell to run the final command that begins with a particular string of characters. For instance, one other option to run the identical find Walden
command can be to kind !loc
(with no house after the exclamation level).
Enter as few characters as you need after the exclamation level. The shell will cease on the first incidence of a string that matches these characters. In the instance simply talked about, I might have typed !lo
since there have been no different instructions that started with these letters. But if I had merely entered !l
, the instance would have run command 331, the ls -l
command, as a result of this could have been the primary match.
Editor’s be aware: Parts of this text had been tailored from The Mac OS X Command Line: Unix Under the Hood, by Kirk McElhearn (2004; reprinted by permission of Sybex). It has been up to date to mirror the adjustments within the Terminal for macOS Big Sur.