With the Terminal in macOS, you can help save a lot of time and sort less by applying the command history functions designed into the Terminal shell. The shell keeps a record of the instructions you operate, and you can access this record with a number of straightforward keystrokes to run commands again or edit them so you do not have to retype them.
You can test to see what instructions are in your history at any time by working the
record command. When you type
record and press Return, Terminal displays a list of the instructions in the historical past checklist. For illustration:
2 cd ..
4 cd kirk
Each and every line involves the variety of the command (from the to start with to the past) and the command itself. Terminal contains all instructions, whether or not or not they had been prosperous. This suggests that faulty or misspelled ones will surface in the historical past.
Limit the heritage list
There are a number of ways of exhibiting your history list. In most scenarios, you want to see the most-modern instructions. Just one way to do this is to run the
history command with an argument that claims how quite a few instructions you want to show.
5 tells the shell to show the commands starting with the fifth one in the historical past checklist. You can enter any variety as an argument for the
history command if you enter a range that’s invalid (most most likely, your checklist is not as lengthy as the range you entered), Terminal will answer with
fc: no these occasion.
As talked over in a separate report, you can shift up or down your background list by urgent the arrow keys. This is the simplest way to rerun a command you executed lately. But if your command is even further back in the checklist, there are quicker methods to convey to the shell which one particular to run.
Say you have exhibited your very long background record, and element of it looks like this:
329 identify Walden
331 ls -l
If you want to re-execute the command
find Walden, type
!329. The exclamation stage (
!) is a shortcut for a command in the historical past list. If you enter a number right after it (with no space involving), Terminal runs the command that has that absolute quantity in the history.
Relative figures: Another way to specify a earlier command is by working with a relative number, or the nth command back from the conclude of the checklist. For example, if your record is 200 entries extended and you want to enter the fifth command back again from the 200th entry, enter
Figures: You can convey to the shell to operate the very last command that begins with a unique string of people. For illustration, an additional way to run the same
locate Walden command would be to form
!loc (with no house immediately after the exclamation position).
Enter as couple of people as you want immediately after the exclamation level. The shell will stop at the to start with prevalence of a string that matches these characters. In the example just described, I could have typed
!lo since there have been no other commands that began with these letters. But if I experienced merely entered
!l, the case in point would have operate command 331, the
ls -l command, for the reason that this would have been the first match.
Editor’s observe: Components of this write-up ended up tailored from The Mac OS X Command Line: Unix Less than the Hood, by Kirk McElhearn (2004 reprinted by authorization of Sybex). It has been up-to-date to mirror the changes in the Terminal for macOS Large Sur.