Searching your command history on macOS terminal

What is the shortcut to search my command history in macOS terminal?

For how long is the history available for searching? Where is it stored?


Solution 1:

How about using Ctrl+R for searching on the Terminal Utility in Mac for searching on the command history,

dudeOnMac: freddy$ whoami
freddy
(reverse-i-search)`who': whoami

Well for controlling how long the history would be retained that depends on a few shell environment variables, HISTFILESIZE which is nothing but number of lines of history you want to retain. Set a huge value for it in .bash_profile for it to take effect

HISTFILESIZE=10000000 

Solution 2:

Use Ctrl + R for searching a command from history in Terminal.

(reverse-i-search)`': 

Type any substring of the command you want to search e.g. grep

(reverse-i-search)`grep': grep "XYZ" abc.txt

It will return the latest command that matches your input. If that is not the command you were searching for, keep pressing Ctrl + R for next match until you find your command.

Once you found your command press Return to execute it.

If you want to exit without running any command, press Ctrl + G

PS: This answer is same as suggested by Inian, just giving more details for easy usage.

Solution 3:

The command history is stored under your home folder in a hidden file called .bash_history. To view it's content in nano, use the following command in Terminal:

nano ~/.bash_history

Or open with your text editor (default is TextEdit):

open ~/.bash_history

In my case it's a very long list and as I scroll through seems like the last ~500 command is stored here.