Last active
September 27, 2025 16:24
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
History search with
fzfUse case
hhand load history withfzfinterfaceDownload and Installation
1. Create directories and download the script:
2. Include
hh.shscript into your~/.zshrc(or~/.bashrc)3. Reload your shell configuration:
For Zsh:
For Bash:
Or simply restart your terminal.
Usage
Now you can use
hhas a command from anywhere:MANDATORY: Install
fzffor enhanced experienceIf you don't have
fzfinstalled:Ubuntu/Debian:
macOS:
or search here