HSTR Makes Searching Your Bash Or Zsh Command History Easy

HSTR

HSTR is a ncurses-based tool for Bash and Zsh that makes it easy to view, navigate and search your command history.

The interactive command line utility aims to make completion easier and more efficient than Ctrl + r, while also allowing you to remove entries from your command history, and bookmark commands for easy access.

When invoked, HSTR displays a list of recently used commands sorted by ranking. You can change the view to show the chronological command history or your favorites using Ctrl + /. Use the Up / Down keys to navigate through your command history, or start typing to find the command you're looking for.

HSTR supports 3 types of filtering between which you can switch using Ctrl + e: keyword search (substrings whose order doesn't matter) which is default, substring-based matching, and regular expression matching. The search is case insensitive by default but can be changed using Ctrl + t.

After you find the command you're looking for, use Enter to run it or press TAB to complete it (and exit HSTR). You can add a command to favorites using Ctrl + f, or remove commands (for example commands that may contain sensitive data) with DEL.

Related: Bookmark Terminal Commands And Easily Retrieve Them Using Marker

The default history view, filtering and case sensitive / insensitive settings can all be changed permanently by exporting the appropriate variable in your ~/.bashrc or ~/.zsh file - you can find the various configuration options here.

You can see a HSTR demo in the following animated GIF (image credits: HSTR GitHub):


Install HSTR


To install HSTR, see its Installation section. You'll find packages for a wide range of Linux distributions, from Ubuntu, Fedora and openSUSE, to Arch Linux, Genoo or CentOS. macOS packages are also available.

Once installed, run the following command (run it only once because it doesn't check if the HSTR configuration was already added) to append its configuration to your ~/.bashrc file and source it:

hstr --show-configuration >> ~/.bashrc
. ~/.bashrc

I'm not sure if this command is also compatible with Zsh, which I don't use. If you use Zsh, you may need to configure hstr manually.

This is what hstr "--show-configuration" command added to my ~/.bashrc file:

# HSTR configuration - add this to ~/.bashrc
alias hh=hstr                    # hh to be alias for hstr
export HSTR_CONFIG=hicolor       # get more colors
shopt -s histappend              # append new history items to .bash_history
export HISTCONTROL=ignorespace   # leading space hides commands from history
export HISTFILESIZE=10000        # increase history file size (default is 500)
export HISTSIZE=${HISTFILESIZE}  # increase history size (default is 500)
# ensure synchronization between Bash memory and history file
export PROMPT_COMMAND="history -a; history -n; ${PROMPT_COMMAND}"
# if this is interactive shell, then bind hstr to Ctrl-r (for Vi mode check doc)
if [[ $- =~ .*i.* ]]; then bind '"\C-r": "\C-a hstr -- \C-j"'; fi
# if this is interactive shell, then bind 'kill last command' to Ctrl-x k
if [[ $- =~ .*i.* ]]; then bind '"\C-xk": "\C-a hstr -k \C-j"'; fi

If you remove HSTR, remember to remove this section from the ~/.bashrc file.

Now you can invoke HSTR by using Ctrl + r in the terminal.

To customize HSTR, including changing its default history view, keyboard shortcut, blacklist commands (hide them from any HSTR view), and so on, visit its configuration page.