
How to look at your command history list in macOS’s Terminal
With the Terminal in macOS, you can save heaps of time and sort less by working with the command history capabilities created into the Terminal shell. The shell keeps a history of the commands you operate, and you can access this historical past with a few easy keystrokes to operate instructions once again or edit them so you never have to retype them.
You can check to see what instructions are in your history at any time by running the heritage
command. When you type history
and push Return, Terminal shows a record of the instructions in the heritage checklist. For case in point:
1 ls
2 cd ..
3 ls
4 cd kirk
Each and every line includes the number of the command (from the to start with to the past) and the command itself. Terminal features all commands, whether or not or not they were successful. This usually means that erroneous or misspelled types will show up in the historical past.
Limit the background checklist
There are many strategies of exhibiting your history list. In most conditions, you want to see the most-recent commands. One way to do this is to run the historical past
command with an argument that states how several instructions you want to show.
For instance, record
5
tells the shell to screen the commands starting off with the fifth one particular in the history list. You can enter any amount as an argument for the heritage
command if you enter a range which is invalid (most possible, your list is not as very long as the number you entered), Terminal will respond with fc: no these kinds of event
.
Time-conserving shortcuts
As mentioned in a separate write-up, you can go up or down your heritage record by pressing the arrow keys. This is the least complicated way to rerun a command you executed not long ago. But if your command is additional back in the checklist, there are more rapidly ways to inform the shell which one particular to run.
Say you have displayed your lengthy record list, and section of it appears like this:
329 track down Walden
330 heritage
331 ls -l
If you want to re-execute the command track down Walden
, type !329
. The exclamation stage (!
) is a shortcut for a command in the heritage list. If you enter a number after it (with no space in between), Terminal operates the command that has that complete range in the history.
Relative figures: Another way to specify a prior command is by working with a relative number, or the nth command back from the conclude of the checklist. For illustration, if your historical past is 200 entries long and you want to enter the fifth command again from the 200th entry, enter !-5
.
People: You can inform the shell to operate the previous command that starts with a distinct string of people. For case in point, yet another way to run the same locate Walden
command would be to form !loc
(with no house soon after the exclamation position).
Enter as handful of characters as you want soon after the exclamation position. The shell will halt at the 1st occurrence of a string that matches these characters. In the illustration just pointed out, I could have typed !lo
since there had been no other instructions that commenced with those letters. But if I experienced simply entered !l
, the instance would have operate command 331, the ls -l
command, for the reason that this would have been the first match.
Editor’s observe: Areas of this article were being tailored from The Mac OS X Command Line: Unix Less than the Hood, by Kirk McElhearn (2004 reprinted by permission of Sybex). It has been up-to-date to mirror the adjustments in the Terminal for macOS Big Sur.