Skip to main content

Bash history command completion improvement

This blog post might be outdated!
This blog post was published more than one year ago and might be outdated!
· 2 min read
Stephan Hochdörfer
Head of IT Business Operations

Following the blog post of Christian Münch in which he describes his setup in regard of improved bash command history management, I thought I'd share my setup as well. For years, I have used a configuration I originally found in Rob Allen's blog. This gives me a context specific history at the bash prompt:

# up & down map to history search once a command has been started.
bind '"\e[A":history-search-backward'
bind '"\e[B":history-search-forward'

Those two bind commands will make sure that my bash history is searched for a match whenever I press ARROW_UP or ARROW_DOWN, e.g when I type in git and press ARROW_UP it will cycle through all commands that start with git. This makes navigating commands a lot easier.

As a downside only commands will match that start with the term I just entered. For most cases this is fine, but sometimes you want to search the whole command for a match. For that, I recently began use another tool called HSTR. HSTR is a cli tool that "brings improved command completion for the bash history command". To install HSTR follow the description in the README file for the Linux distribution of your choice or build it yourself.

Once installed and configured, it can be invoked with CRTL + R and allows you to easily search and navigate in the search results. In contrast to Rob Allen's configuration, HSTR will search in the whole command for a match. That means if I search for "ps", HSTR will find the "docker ps" command as well as "ps aux" command.