zsh

ZSH History Substring Search for Efficient Command Recall

Z Shell, commonly known as ZSH, is an incredibly powerful and highly customizable Unix shell that is built on top of Bash. It comes packed with a wide variety of features that help to take a simple shell into a full-fledged productivity tool.

One of the standout features of ZSH and the Oh My ZSH framework is the ability to perform the command history substring search. This feature allows us to search and recall the previous commands in an efficient and performance friendly feature.

In this guide, we will walk you through how you can enable and configure the history substring search on the native ZSH or using the Oh My ZSH plugins.

What Is ZSH History Substring Search?

For us, developers, we mostly spend a lot of times in the terminal and we sometimes repeat various commands. For example, remember how many times you start a web server, especially without hot-reload?

This is where the history substring search comes into aid. In ZSH, this feature is abbreviated as HIST_SUBSTRING_SEARCH. It is a feature that allows us to search through the command history by simply typing a subsection of the command that we wish to find.

Instead of memorizing the entire command syntax, you can use a subsection of the command and re-run it in a simple technique.

This makes recalling and reusing the commands from your history incredibly efficient.

Advantages of the History Substring Search

There are obvious advantages to using the history substring search feature. Some of them include:

  1. Faster and efficient command reuse – Using this feature, you can find and re-run the command quickly without traversing your entire command history.
  2. Reduced typing – This one is pretty obvious. Using the substring search, you only need to type a small part of the previous command. This can save you time and effort. If you want to type, focus on the code not on commands.
  3. Improved productivity – As you can guess, it allows us to maintain a clean and organized command history which we can use to export into scripts.

Enable the ZSH History Substring Search

Before we can use the history substring search feature, we need to enable it in our ZSH shell. If you are using the native ZSH without the Oh My ZSH framework, keep reading. Otherwise, skip to the next section where we discuss how to enable it as a plugin in Oh My ZSH.

Edit the ZSH configuration file.

$ nano ~/.zshrc

Add the following entries in the configuration file:

autoload -Uz up-line-or-beginning-search

zle -N up-line-or-beginning-search

bindkey '^[[A' up-line-or-beginning-search

autoload -Uz down-line-or-beginning-search

zle -N down-line-or-beginning-search

bindkey '^[[B' down-line-or-beginning-search

In the given entries, we define the key bindings to search up and down the history using the “^[[A” and “^[[B” keys, respectively.

If you do not wish to use these key bindings, feel free to change them to your specific needs.

Customizing the Search Behavior

You can configure the behavior of the history substring search further by modifying the ZSH options. For example, to make the search case-insensitive, add the following entry in the configuration file:

setopt HIST_SUBSTRING_SEARCH_IGNORE_CASE

To always receive unique search results, use the following command:

setopt HIST_SUBSTRING_SEARCH_IGNORE_ALL_DUPS

You can explore the docs for more configuration options.

Limiting the Search to Specific Time Frames

If you want to search within a specific time frame of the command history, you can use the “-t” option with up-line-or-beginning-search.

bindkey '^[[A' up-line-or-beginning-search -t -1w

This restricts the search to the past week’s commands.

Search and Execute

You can combine the history substring search with command execution in a single step. For example, execute the selected command immediately after finding it.

bindkey '^[[A' up-line-or-beginning-search; zle accept-search

The given entry binds the up-line-or-beginning-search and accept-search functions together.

Enable the Oh My ZSH History Substring Search

When it comes to Oh My ZSH, we can implement the history substring search using the zsh-history-substring-search plugin.

You can learn more about this plugin in the following link:

https://github.com/zsh-users/zsh-history-substring-search

However, this requires you to have the ZSH version 4.3 and above and installed OMZ.

To install it as an Oh My ZSH plugin, start by cloning the repository with the command as follows:

git clone https://github.com/zsh-users/zsh-history-substring-search ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-history-substring-search

Next, edit the ZSH configuration file to activate the plugin:

$ nano .zshrc

Add the name of the plugin into the array as follows:

plugins=( [plugins...] zsh-history-substring-search)

Lastly, run the following command to apply the changes:

$ exec zsh

Customization

You can configure the plugin by understanding the following parameters. Feel free to explore the docs for more.

  • The HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND is a global variable that controls how a matching query is highlighted inside a found command, typically with bold white text on a magenta background.
  • The HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND is a global variable that specifies how a query is highlighted when no command in the history matches it, often with bold white text on a red background.
  • The HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS is a global variable that determines how the command history is searched for your query. By default, it performs a case-insensitive search.
  • The HISTORY_SUBSTRING_SEARCH_FUZZY is a global variable that, when set, enables the fuzzy searching in the command history. It matches the words in the given order, e.g., “ab c” matches “abc*”.
  • The HISTORY_SUBSTRING_SEARCH_PREFIXED is a global variable that defines whether your query should match only at the start of each history entry. If set, “ls” will match “ls -l” but not “echo ls”.
  • The HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE is a global variable that controls whether all returned search results are unique. When set, only the unique search results are presented. This behavior is off by default but can be enabled to ensure uniqueness.
  • The HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_TIMEOUT is a global variable that defines a timeout in seconds to clear the search highlight after performing a search.

Conclusion

In this tutorial, we learned how we can enhance the terminal productivity in the terminal by taking advantage of the ZSH shell and the history substring search feature to search throughout the terminal history.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list