With the Terminal in macOS, it can save you plenty of time and kind much less through the use of the command historical past features constructed into the Terminal shell. The shell retains a report of the instructions you run, and you may entry this historical past with a couple of easy keystrokes to run instructions once more or edit them so that you don’t need to retype them.
You may verify to see what instructions are in your historical past at any time by working the historical past
command. Once you sort historical past
and press Return, Terminal shows a listing of the instructions within the historical past checklist. For instance:
1 ls
2 cd ..
3 ls
4 cd kirk
Every line consists of the variety of the command (from the primary to the final) and the command itself. Terminal consists of all instructions, whether or not or not they have been profitable. Which means that inaccurate or misspelled ones will seem within the historical past.
Restrict the historical past checklist
There are a number of methods of displaying your historical past checklist. Generally, you wish to see the most-recent instructions. A method to do that is to run the historical past
command with an argument that claims what number of instructions you wish to show.
For instance, historical past
5
tells the shell to show the instructions beginning with the fifth one within the historical past checklist. You may enter any quantity as an argument for the historical past
command; in the event you enter a quantity that’s invalid (more than likely, your checklist 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 checklist by urgent the arrow keys. That is the simplest strategy to rerun a command you executed lately. But when your command is additional again within the checklist, there are faster methods to inform the shell which one to run.
Say you could have displayed your lengthy historical past checklist, and a part of it appears like this:
329 find Walden
330 historical past
331 ls -l
If you wish to re-execute the command find Walden
, sort !329
. The exclamation level (!
) is a shortcut for a command within the historical past checklist. If you happen to enter a quantity after it (with no area between), Terminal runs the command that has that absolute quantity within the historical past.
Relative numbers: One other strategy to specify a earlier command is through the use of a relative quantity, or the nth command again from the tip of the checklist. For instance, in case your historical past is 200 entries lengthy and also you wish to enter the fifth command again from the 2 hundredth entry, enter !-5
.
Characters: You may inform the shell to run the final command that begins with a particular string of characters. For instance, one other strategy to run the identical find Walden
command could be to sort !loc
(with no area 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. Within the instance simply talked about, I might have typed !lo
since there have been no different instructions that started with these letters. But when I had merely entered !l
, the instance would have run command 331, the ls -l
command, as a result of this might have been the primary match.
Editor’s be aware: Elements of this text have been tailored from The Mac OS X Command Line: Unix Underneath 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 Massive Sur.