I'm looking for a tool to allow reverse incremental searching with simple regular expression (or just multiple matches) support. For example, if I want to find the command 'foo bar baz', I could do something like the following to find the command quickly:
CRTL-R (starts search)
type 'foo' (matches most recent command using foo)
continue typing 'foo|baz' (matches most recent command containing 'foo' AND 'baz'.
Does something like this exist? If no, how could I go about implementing it myself?
Best Answer
Note that this answer is outdated and Sergey Romanovsky gives a much better one. I cannot delete this one because it is marked accepted, but please note it now serves more as a basic illustration of zsh's widget programming.
Custom widget
history-incremental-multi-search
forzsh
Setup
Create a directory and include it in your
$fpath
For example, I created a directory~/.zsh/functions
, and the linefpath=($HOME/.zsh/functions $fpath)
in my.zshrc
.Put the following in a file named
history-incremental-multi-search
in that directory.Put this in or source it from your
.zshrc
:Use
You should now be able to initiate a backward incremental search with Ctrl+X, r, forward with Ctrl+X, s.
Type your search terms separated by space. Following keys are available to control it:
← Backspace: erase character
Ctrl+W: erase word
Ctrl+U: kill line
Ctrl+N: next match
Ctrl+P: previous match
Ctrl+G/Esc: cancel search
Enter: accept
This solution can probably be simplified quite a bit. It's more a functional proof of concept, with lots of room for improvement.