Guu
Guu

Reputation: 971

Filtering ZSH history by command

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?

Upvotes: 63

Views: 48219

Answers (7)

lolesque
lolesque

Reputation: 12010

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.

Upvotes: 113

jdhao
jdhao

Reputation: 28467

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!

Upvotes: 9

Swaps
Swaps

Reputation: 1568

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

Upvotes: 2

Konrad Haenel
Konrad Haenel

Reputation: 61

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

Upvotes: 6

mihai
mihai

Reputation: 4742

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.

Upvotes: 7

zjaquish
zjaquish

Reputation: 271

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".

Upvotes: 12

Frost
Frost

Reputation: 11977

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.

Upvotes: 4

Related Questions