Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 704
Guu Avatar asked Dec 26 '12 12:12

Guu


People also ask

How do you search ZSH history?

To use it, press CTRL + R in your terminal session. This will change your terminal session to search mode, and you can type for previous commands. As you type, the shell will search for a matching command in the history and suggest it. To search for the next matching suggestion, press CTRL + R.

How do I turn off history on ZSH?

enablehistory just unsets function set by previous alias. Show activity on this post. Try configuring your ZSH setup to run a postexec function that empties/deletes your .

How do I change my ZSH history size?

Increasing the History File Size Unfortunately, zsh's default history file size is limited to 10000 lines by default and will truncate the history to this length by deduplicating entries and removing old data. Adding the following lines to . zshrc will remove the limits and deduplication of the history file.


4 Answers

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.

like image 99
lolesque Avatar answered Oct 17 '22 16:10

lolesque


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

like image 40
zjaquish Avatar answered Oct 17 '22 16:10

zjaquish


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.

like image 37
mihai Avatar answered Oct 17 '22 18:10

mihai


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!

like image 35
jdhao Avatar answered Oct 17 '22 17:10

jdhao