A better and quicker way than “history | grep” to cycle through your command history in Bash or Zsh is by using reverse search, which is built into most shells. Here are some efficient shortcuts:
1. Reverse Search (Ctrl + R)
- Press
Ctrl + R
and 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 + R
to 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 + R
to cycle through moregit
commands 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
~/.bashrc
to search commands starting with the characters you’ve typed usingUp
andDown
arrow 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’s History Search (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.
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
~/.bashrc
or~/.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.
tl;dr – quicker 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.
These methods should save you time and eliminate the need for history | grep!