107

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?

codeforester
  • 39,467
  • 16
  • 112
  • 140
Aipi
  • 2,076
  • 4
  • 18
  • 27

13 Answers13

129

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 
Inian
  • 80,270
  • 14
  • 142
  • 161
  • 11
    Also worth noting, `ctrl`+`R` shows the last, but hitting `ctrl`+`R` multiple times will give you the matches before it. – Matteo Ferla Oct 29 '20 at 13:37
  • I was looking for this exact thing! -- how to see multiple matches – Kanad Jan 12 '23 at 11:25
  • The default history search provided by bash doesn't provide multiple matches. You need a higher level plugin like fzf (fuzzy finder) – Inian Jan 12 '23 at 11:27
73

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.

Sahil Chhabra
  • 10,621
  • 4
  • 63
  • 62
  • 2
    while we keep pressing ctrl + R, what is the reverse of ctrl + R ? like If I am keep pressing ctrl + R and going one by one reverse in history and now If I want to come forward one by one command in mac terminal what is the command? in linux machine I used to use CTRL + SHIFT + R to do the same but for mac its not working. any other shortcut we need to use for this use case? – NikhilP Dec 19 '22 at 09:22
  • 1
    @NikhilP in Mac the reverse of `ctrl` + `R` would be `ctrl` + `S` – johnnyasd12 May 11 '23 at 09:04
25

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.

balazs630
  • 3,421
  • 29
  • 46
  • @balazs630 nano is an editor, and should not be used to read files with no intention of editing. At the very least, doing so creates risk of accidental editing. Instead use less, which is a command line interactive application that provides read access with no editing capability. – MasterOfNone Sep 04 '20 at 18:17
  • 3
    History is stored in ~/.zsh_history or ~/.bash_history or ~/.history depending on your shell. – duplex143 Jun 10 '21 at 12:40
  • 3
    it would be `~/.zsh_history` if you use zsh. I was in awe that it stores every single command I typed since I installed zsh – hungdoansy May 31 '22 at 06:47
19

Migrating an answer to SO from this answer on the Unix and Linux Stack Exchange:

Pressing ctrl+R will open the history-search-backward. Now start typing your command, this will give the first match. By pressing ctrl+R again (and again) you can cycle through the history.

If you like to be super lazy you can bind the up/down arrow keys to perform this search, I have the following in my .inputrc to bind the up/down arrow key to history-search-backward and history-search-forward:

# Key bindings, up/down arrow searches through history
"\e[A": history-search-backward
"\e[B": history-search-forward
"\eOA": history-search-backward
"\eOB": history-search-forward

Just type something (optional), then press up/down arrow key to search through history for commands that begin with what you typed.

To do this in .bashrc rather than .inputrc, you can use the syntax:

bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'
bind '"\eOA": history-search-backward'
bind '"\eOB": history-search-forward'

Update for ZSH on Mac

For newer MacOS laptops that no longer support bash, you can add the following to your ~/.zshrc file to accomplish the same result:

bindkey "\e[A" history-search-backward
bindkey "\e[B" history-search-forward
bindkey "\eOA" history-search-backward
bindkey "\eOB" history-search-forward

then update your terminal session with the command source ~/.zshrc to have your changes take effect immediately

matt123788
  • 419
  • 5
  • 16
  • I tried adding bashrc, but it shows .bashrc:1: command not found: bind – sktguha Sep 18 '20 at 06:18
  • This may also require to change iTerm2 key bindings for up and down keys accordingly: – Denis Jan 27 '22 at 11:39
  • Actually, this command allows cycling through whole history, not just the commands that begin with what I typed – Falco Peregrinus Feb 22 '22 at 20:57
  • what is the reverse of ctrl + R ? like If I am doing pressing ctrl + R and going one by one reverse in history and now If I want to come forward one by one command in mac terminal what is the command? in linux machine I used to use CTRL + SHIFT + R to do the same but for mac its not working. any other shortcut we need to use for this use case? – NikhilP Dec 19 '22 at 09:21
  • 1
    @NikhilP The reverse of `Ctrl + r` (i.e. searching **forward**) is `Ctrl + s`. Checked on macOS Ventura 13.1 (with Zsh) - it works. See the following answer for more details: https://stackoverflow.com/a/791800/814702. – informatik01 Jan 04 '23 at 19:18
16

Use this command -

  history

This works on both OSX and Linux.

History is stored in ~/.zsh_history or ~/.bash_history or ~/.history depending on your shell.

History is stored for 1000 or 2000 lines depending on your system.

 echo $HISTSIZE
manas sharma
  • 463
  • 1
  • 6
  • 8
9

You can also try the following:

history | grep 'git'

Where 'git' is the command you are looking for.

Pontios
  • 2,377
  • 27
  • 32
4

For those who want to search specific command from history, you can do so with reverse-i-search. Reverse search allow you to type in any key words(any) that is part of the command you are looking for and reverse search navigate back to history, match previous commands incrementally and return the entire command.

It is especially useful as when one cannot remember all handy lengthy commands they use often. To do reverse-search ctrl + R and type any clue you have and that will return your previous commands matching the words you type. Then once found the command, hit Enter to execute it directly from search.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Hung Om
  • 630
  • 7
  • 16
2

Automation AppleScript

Since you mentioned viewing your history as a quick solution, via the Terminal.app. You might want to automate, or quickly view history, maybe from the dock. You may use the AppleScript application as one alternative. This is an optional approach to create a simple shortcut, as to many others.

  1. Open the AppleScript editor application.
  2. Add your specified commands, for history.
  3. Code
tell application "Terminal"
        do script "history"
end tell
  1. Save as application, drag to dock for convenience.

History Storage & Time Stored Details

HISTSIZE Determines how many lines will be written to the history file.

HISTFILESIZE Determines how long the file.

Find out how long history is stored:

echo $HISTSIZE $HISTFILESIZE

Note: You may also increase your command history storage size in the length of two variables. You may achieve this through HISTSIZE and HISTFILESIZE environment variables which are located in your ~/.bash_profile file.

It is possible to achieve this by modifying ~/.bash_profile, the number placeholder with SIZE represent's the number, lines value as example:

export HISTFILESIZE=SIZE # Example 1000
export HISTSIZE=SIZE # Example 10000

Pre macOS 11 Big Sur

cat ~/.bash_history

HISTFILESIZE will only set a maximum history value which is stored to the history file when a session is started. HISTSIZE will determine specifically how many lines will be stored or in other words, written at the end of the session. If the set HISTFILESIZE is determined to be a large value than what HISTSIZE is set, you will not view history larger than your set HISTSIZE. The reason is that the history file is overwritten with the HISTSIZE unless using histappend option turned ON.

You may use also histappend to append history, If the histappend shell option is turned on lines are appended to the history file. Otherwise, the overwritten alternative proceeds.

Bash GNU - histappend

macOS 11 Big Sur

nano ~/.zprofile

Modify history environment variables, set to a value:

export HISTFILESIZE=1000
export HISTSIZE=SIZE=1000

Run the source command can be used to load any functions file into the current shell script or a command prompt.

source ~/.zprofile
echo $HISTSIZE $HISTFILESIZE

Outputs:

1000 1000

Output where some history is stored:

cat ~/.zsh_history
ABC
  • 2,068
  • 1
  • 10
  • 21
2

history ~ works for me on Ventura 13.4.1

1

For macOS Big Sur the file is now .zsh_history

If you do vi ~/.zsh_history in the terminal you can use regex by pressing the / and then the search term.

0

To review or recall recently used commands, you can just press the up arrow key to sequentially read back through the history stored in .bash_history.

Cam_Aust
  • 2,529
  • 2
  • 19
  • 28
0

To search through history with ease, I advise you to install fzf.

It's an interactive Unix filter for command-line that can be used with any list; files, command history, processes, hostnames, bookmarks, git commits, etc.

Just install it, click ctrl + R, and you'll be to scroll through you shell history, without the need to grep or waiting ages until the command you're waiting for pops up.

It supports Mac OS, Linux and even Windows.

hilsenrat
  • 1,390
  • 2
  • 8
  • 21
0
# USAGE: find.history cd
# filter commands in shell history by a search term and execute the selected command
function find.history {
  eval $(history | grep "$1" | tail | awk '{$1=""}1' | tail -r | peco)
}

You will need to have peco installed.

https://github.com/peco/peco

[$]> brew install peco

Andrew
  • 3,733
  • 1
  • 35
  • 36