Improved incremental search

FelixRosencrantz: Some thoughts on incremental Search… What is Incremental Search?

The shell performs an incremental search by searching for a specified string in the shell's history. As the user types each character of the search string the shell gives immediate feedback changing the current command line to the history line and cursor position that matches the search string. The shell will only jump to a new spot if the search string no longer matches at the current spot. The shell provides a mini-buffer (separate from the command line) that shows the current search string. The search starts from the current point in history and searches in one direction either the start or end of history. The shell starts at the current cursor position and searches towards one end of the history.

The mini-buffer has a prompt string that reports status of the search directory and if the search string is not found. The search is case-insensitive, and can start with a “^” to say the search string should match the beginning of a history line. The zle widgets can take an argument which is used as the initial input for the search string.

From the manual: “A restricted set of editing functions is available in the mini-buffer. An interrupt signal, as defined by the stty setting, will stop the search and go back to the original line. An undefined key will have the same effect. The supported functions are: backward-delete-char, vi-backward-delete-char, clear-screen, redisplay, quoted-insert, vi-quoted-insert, accept-and-hold, accept-and-infer-next-history, accept-line and accept-line-and-down-history.

magic-space just inserts a space. vi-cmd-mode toggles between the 'main' and 'vicmd' keymaps; the 'main' keymap (insert mode) will be selected initially. history-incremental-search-backward will get the next occurrence of the contents of the mini-buffer. history-incremental-search-forward inverts the sense of the search. vi-repeat-search and vi-rev-repeat-search are similarly supported. The direction of the search is indicated in the mini-buffer.

Any multi-character string that is not bound to one of the above functions will beep and interrupt the search, leaving the last found line in the buffer. Any single character that is not bound to one of the above functions, or self-insert or self-insert-unmeta, will have the same effect but the function will be executed.”

Incremental search is performed by the following zle widgets:

Incremental search is (probably) inspired/copied from Emacs's isearch functionality. Only some of that functionality has been copied into the Z-shell (or other shells for that matter.) So some of the functionality requested here can be directly related to what Emacs has but has been duplicated. Other functionality is different, since the context is different. In Emacs the user is moving around within a file, while within the shell the user is looking at discrete history lines.

Aspects of Incremental Search:

What Could be Improved. Bart points out that the ultimate thing to do here would be to rip out the incremental search builtins entirely and write the whole thing in shell code around the read-from-minibuffer widget. This would allow for greater customization as outlined below. The current mechanism is somewhat limited to it's current functionality, and could provide more ways to add widgets. For example, now only certain widgets can be used in incremental-search mode, and there is no way to add a new widget to be used in this mode.

Links:


Already Added:

Ability to use a zsh parameter to examine the previous search string/set the search string. – FelixRosencrantz?