Linux Commands

How to Use Hstr Command History Browser in Linux

This article will cover a guide on using the “hstr” command line utility that can be used to maintain, manage, browse and navigate command history of bash and zsh shells. It is more advanced than the “history” command available in all major Linux distributions by default and features numerous additional features.

Main Features of Hstr Command Line Tool

Hstr command can be configured to provide search suggestions while typing, similar to suggestions you typically see in a web browser and other applications featuring auto-completion and fuzzy search. It also predicts your command usage patterns and suggests your frequently used commands more often and keeps them at top. You can also manually add commands to favorites or add them as bookmarks for easy and quick access. Other main features of Hstr include ability to remove commands from history, built-in keyboard shortcuts, colored output, support for regex patterns, and so on.

Installing Hstr in Linux

You can install Hstr in Ubuntu using the command mentioned below:

$ sudo apt install hstr

You may find a more up-to-date version of Hstr for Ubuntu in the official PPA repository provided by developers of the Hstr command line utility. To install Hstr from the PPA repository, follow instructions available here.

Hstr may be available in default repositories of other Linux distributions, so you can try to install it from the package manager. Installable packages for more than 15 Linux distributions and further installation instructions are available here.

Using Hstr Command

To use Htsr command line utility, just type the following command in a terminal emulator:

$ hstr

Depending on your bash or zsh command line history, you should see some output similar to this:

te the history entries using arrow keys and hit <Enter> key to pick the highlighted command in the history. The results below the white bar will change as you type, showing you only relevant commands available in the history matching the entered characters. The white bar also shows you certain keyboard shortcuts that can be used to change view type. For instance, you can switch between favorites, frequently used, and recent results by pressing <CTRL+/> key.

You can exit the history view anytime by pressing <CTRL+C> key. To view only favorites by default, you can run the following command:

$ hstr- f

To view command history in a non-interactive manner, run the following command:

$ hstr -n

Adding a Command to Favorites

You can add a command to favorites for easy access by pressing <CTRL+F> key while a command is highlighted. You will get a message in the terminal indicating that a command has been successfully added to the favorites list.

You can view favorites by pressing <CTRL+/> key multiple times until the list of favorites appears under the white bar.

Configuring Hstr Command

You can view optimized configuration settings for most use cases by running the following command:

$ hstr --show-configuration

The configuration dump is self-explanatory and well commented, you can know more about each configuration parameter by reading its respective comment. These optimized settings are not enabled by default, you will need to add them to the “$HOME/.bashrc” file. To do so, run the following two commands:

$ hstr --show-configuration >> "$HOME/.bashrc"
$ source "$HOME/.bashrc"

Once these settings are saved to the bashrc file, you will be able to use <CTRL+R> keyboard shortcut to directly invoke hstr command from a terminal emulator.

These settings should be enough for most use cases. However, if you further want to tweak configuration of the hstr command, use this detailed guide available on the official Hstr wiki. This guide also contains useful examples and configuration presets that you can put in bashrc or zshrc files.

Blacklisting Certain Commands from Appearing in Hstr Results

Blacklisting function is disabled by default in Hstr. To enable it, add the following line to “$HOME/.bashrc” file:

export HSTR_CONFIG=blacklist

If the HSTR_CONFIG environment variable already has a value assigned to it, you can add more values on the right hand side by using the comma symbol as delimiter. After adding the above line, run the following command to apply the changes made to the bashrc file:

$ source "$HOME/.bashrc"

Now that blacklisting is enabled, you can add commands to the “$HOME/.hstr_blacklist” file to blacklist them. The blacklist file should contain one command per line.

Conclusion

Hstr command provides many useful features over the classic history command available in most Linux distributions. Not only it makes browsing command line history more user friendly, it also allows you to hide sensitive commands by adding them to blacklist.

About the author

Nitesh Kumar

I am a freelancer software developer and content writer who loves Linux, open source software and the free software community.