Getting good at zsh history search will save you more time than almost any other terminal habit. Instead of piping history into grep and scrolling through results, use the reverse search and arrow-key tools already built into Bash and Zsh. Here are the fastest ways to search your command history in both shells, without ever typing “history | grep” again.
Table of Contents
1. Reverse Search (Ctrl + R)
- Press
Ctrl + Rand start typing part of the command you’re looking for. - The shell will show you the most recent match from your history.
- You can keep pressing
Ctrl + Rto cycle through previous matches. - When you find the command you want, press Enter to run it or Right Arrow to edit it.
Example:
- Press
Ctrl + R, then typegit. - It shows:
git status - Keep pressing
Ctrl + Rto cycle through moregitcommands from your history.
2. History Search with Arrow Keys (Bash-specific)
You can enable a feature in Bash that allows you to search the command history with arrow keys based on what you’ve already typed.
- Add the following to your
~/.bashrcto search commands starting with the characters you’ve typed usingUpandDownarrow keys:
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'
Now, if you type git and press Up Arrow, it will cycle through commands in history that start with git.
3. Zsh History Search with Arrow Keys
In Zsh, you can configure history search behavior as well. Add this to your ~/.zshrc:
bindkey "^[[A" history-beginning-search-backward
bindkey "^[[B" history-beginning-search-forward
This makes Up and Down arrow keys cycle through commands starting with what you’ve typed – a fast, native form of zsh history search that needs no extra tools.
4. fzf for Interactive Search
For an even more powerful search tool, you can install fzf (fuzzy finder) and bind it to Ctrl + R to get an interactive fuzzy search through your command history.
- Install
fzf:
brew install fzf # For Mac
sudo apt install fzf # For Ubuntu/Debian
- Add this to your
~/.bashrcor~/.zshrc:
# Enable fuzzy search for history
export FZF_DEFAULT_COMMAND='history -1000'
Now, pressing Ctrl + R opens an interactive fuzzy search for your history. You can type any part of the command, and it will display matching history entries as you type.
5. Making Zsh History Search Persistent Across Sessions
None of this is much use if your history keeps disappearing between terminal sessions. By default, Zsh only saves a limited number of commands, and separate terminal tabs don’t always share history with each other. A few settings in ~/.zshrc fix this and make zsh history search genuinely useful across every session:
HISTFILE=~/.zsh_history
HISTSIZE=10000
SAVEHIST=10000
setopt SHARE_HISTORY
setopt HIST_IGNORE_DUPS
setopt HIST_IGNORE_SPACE
- HISTSIZE / SAVEHIST: how many commands are kept in memory and written to disk. 10,000 is a generous starting point for most people.
- SHARE_HISTORY: makes every open terminal see commands from every other open terminal in real time, so a zsh history search in one tab finds commands typed in another.
- HIST_IGNORE_DUPS: stops the same command being logged over and over, which keeps reverse search results clean.
- HIST_IGNORE_SPACE: lets you prefix a command with a space to keep it out of history entirely – handy for one-off commands containing passwords or tokens.
Bash has equivalent options – HISTSIZE, HISTFILESIZE, and shopt -s histappend combined with a PROMPT_COMMAND that runs history -a after every command will get you similar cross-session behaviour, though it’s less seamless than Zsh’s built-in sharing.
tl;dr – quicker than history | grep :
- Ctrl + R: Fast reverse search (default in Bash and Zsh).
- Arrow key history search: Customizable for incremental search based on current input.
- fzf: Fuzzy searching with better visual feedback and flexibility.
- SHARE_HISTORY: makes zsh history search work across every open terminal, not just the current one.
Set up properly, zsh history search should save you time on every terminal session and eliminate the need for history | grep for good.

Leave a Reply
You must be logged in to post a comment.