Filtering ZSH history by command

FilteringHistoryZsh

Filtering Problem Overview


I was told that in ZSH you could do something like command and then when you hit up it would filter the history based on the given command. But when I try this it just cycles the history like bash does. Is this disabled by default?

Filtering Solutions


Solution 1 - Filtering

Hit Ctrl+R, type some letters, it will find the previous command with these letters, keep hitting Ctrl+R to continue through the previous findings.
Works in bash, zsh (and other shells i suppose).

What i personally like to have is: type some letters, press Up, the previous commands starting with the same letters appear. Very powerful, i love it.
You have to bind the keys you want to history-beginning-search-backward and history-beginning-search-forward.

In case it's not enough for you, zsh has a lot of options, try to look in Zsh Line Editor and tell us.
For bash, less powerful but more common, Bash commands for history.

Solution 2 - Filtering

Use exclamation point:

> !<starts-with this string>

You can arrow up/down through all commands that started with that. I use "!v" all the time to get my previous command for opening a file with Vim.

You can also use a question mark to search beyond matching the beginning of the string,

> !?status

Can find "git status".

Solution 3 - Filtering

Use percol to dynamically search and navigate through your history with Ctrl-r.

  • install percol: sudo pip install percol
  • add the zsh-history-search code snippet to your .zshrc file.

After a Ctrl-r, you can see your whole history in the same window. Searching for a keyword (dynamically) narrows that list down. You can use key-bindings (like this emacs like config) to navigate up and down the list and eventually make a selection.

Here is a search for all sudo install commands available in history with sudo make install selected.

percol_history_search_example

Enter issues the selected command.

Solution 4 - Filtering

With the vim keybindings activated in zsh you can use vi-history-search-backward (/) when in vi command mode.

Solution 5 - Filtering

That's a feature available in fish, but it seems like someone made a zsh plugin for it. It's not available in standard ZSH.

Solution 6 - Filtering

We can also use fzf to fuzzy search the command history interactively.

Here is how to install:

git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install

Say yes to all its configs. After that, restart your zsh shell, and press Ctrl-R, whoa, interactive command history search pops up. Enjoy!

Solution 7 - Filtering

If you don't want to add any keybindings, you can search by default using Ctrl+R to move backward & Ctrl+S to move forward. > You can start search with any of the key shortcuts Ctrl+R or Ctrl+S

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGuuView Question on Stackoverflow
Solution 1 - FilteringlolesqueView Answer on Stackoverflow
Solution 2 - FilteringzjaquishView Answer on Stackoverflow
Solution 3 - FilteringmihaiView Answer on Stackoverflow
Solution 4 - FilteringKonrad HaenelView Answer on Stackoverflow
Solution 5 - FilteringFrostView Answer on Stackoverflow
Solution 6 - FilteringjdhaoView Answer on Stackoverflow
Solution 7 - FilteringSwapsView Answer on Stackoverflow