Skip to content

Instantly share code, notes, and snippets.

@andkirby
Last active September 27, 2025 16:24
Show Gist options
  • Save andkirby/4ebda9ff1bd1362c0affb27bea0e01fd to your computer and use it in GitHub Desktop.
Save andkirby/4ebda9ff1bd1362c0affb27bea0e01fd to your computer and use it in GitHub Desktop.
Interactive history search function that uses fzf for enhanced filtering and selection when available, or falls back to grep for basic search. Allows filtering history by keywords and optionally executing selected commands.
#!/usr/bin/env bash
# hh - Enhanced History Search
#
# Description:
# Interactive history search function that uses fzf for enhanced filtering
# and selection when available, or falls back to grep for basic search.
# Allows filtering history by keywords and optionally executing selected commands.
# Compatible with both bash and zsh shells.
#
# Usage:
# hh [search_terms...]
#
# Arguments:
# search_terms - Optional keywords to filter history entries
#
# Examples:
# hh # Show all history (fzf) or no filter (grep)
# hh git # Filter history for commands containing "git"
# hh git commit # Filter for commands containing both "git" and "commit"
# hh "docker run" # Filter for commands containing exact phrase
#
# Behavior:
# - With fzf: Opens interactive fuzzy finder with optional pre-filter
# - Navigate with arrow keys or type to further filter
# - Press Enter to select a command
# - Prompts for confirmation before executing
# - Supports colored output for better readability
# - Without fzf: Uses grep to filter and display matching history entries
# - No execution capability, display only
#
# Dependencies:
# - fzf (optional) - for enhanced interactive experience
# - Standard shell tools (history, grep, sed)
#
# Notes:
# - Compatible with both bash and zsh
# - Temporarily ignores commands starting with space to avoid cluttering history
# - Automatically detects fzf availability and adapts behavior
# - Preserves original functionality when fzf is not installed
# Enhanced history search function
hh () {
setopt histignorespace
if command -v fzf > /dev/null 2>&1
then
autoload -U colors && colors
local query="$*"
local cmd=$(history | \
tail -r | \
sed 's/^[ ]*[0-9*]*[ ]*//' | \
awk '!seen[$0]++' | \
fzf --query="$query" --reverse --header 'Select command to paste to command line')
if [[ -n "$cmd" ]]
then
print -z "$cmd"
fi
else
omz_history | grep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn,.idea,.tox,.venv,venv} "${1:-}"
fi
unsetopt histignorespace
}
@andkirby
Copy link
Author

andkirby commented Sep 8, 2025

image

History search with fzf

Use case

  1. call hh and load history with fzf interface
  2. type key words
  3. find command
  4. hit Enter
  5. get your in command in terminal

Download and Installation

1. Create directories and download the script:

mkdir -p ~/.config/hh
curl -os ~/.config/hh/hh.sh https://gist.githubusercontent.com/andkirby/4ebda9ff1bd1362c0affb27bea0e01fd/raw/da32e2d0a1534e0e7b0e275196655c686380b512/hh.sh

2. Include hh.sh script into your ~/.zshrc (or ~/.bashrc)

echo "# >>> history search >>>" >> ~/.zshrc
echo "source ~/.config/hh/hh-simple.sh" >> ~/.zshrc
echo "# <<< history search <<<" >> ~/.zshrc

3. Reload your shell configuration:

For Zsh:

source ~/.zshrc

For Bash:

source ~/.bashrc

Or simply restart your terminal.

Usage

Now you can use hh as a command from anywhere:

hh                            # Show all history with fzf (or grep if fzf not available)
hh git                       # Filter for commands containing "git"
hh docker run         # Filter for commands containing "docker" and "run"

MANDATORY: Install fzf for enhanced experience

If you don't have fzf installed:

Ubuntu/Debian:

sudo apt install fzf

macOS:

brew install fzf

or search here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment