Incremental searching is a nifty user interface technique in computing. It allows the user to search while typing in what to search for. As such it is particularly efficient and even pleasant for the user. After all, if you're searching for "arieh" and you know only one "ari*", why should you have to type in those 2 extra characters? On the other hand, if it turns out you also know an "ariels", you just hit an extra "eh" to get the right one.

The oldest program I know of which offers incremental search is the venerable Emacs; consequently, it is available virtually unchanged also in XEmacs. However, it may well be much older than that. Later on, a program called Tornado Notes (for MS-DOS) offered this functionality for searching a large collection of notes. Currently, Mozilla and Mozilla Firefox offer increment searching of pages for links and text with a particularly pleasant interface (for browsing -- but alas not for writing in Everything2!), VIM extends the regular vi search to be incremental too, and even some IDEs such as Eclipse and Source Insight have begun to pick up this new user interface technique -- almost 20 years after it was first seen!

How it works

An example probably makes the most sense for explaining incremental search. I'll use Emacs keybindings. Emacs uses C-s (ctrl+S, "isearch-forward") to initiate this search; Windowsified programs will doubtless use some other keystroke (often Ctrl+I which won't interfere with the useless "Save" functionality traditionally bound there to Ctrl+S).

Here's the text to search; point (the cursor position) will always be indicated by pipe "|". Note that cursors are logically between characters, not on them, as point explains. In case of incremental search, point will always be just after the found string.

So let's start searching for "arieh" in the test below. The search is case insensitive in this example...

|After drinking, King Arthur lowered his visor. He searched around and saw Puck, Ariel, and the noders ariels and arieh.
I-search:
We've just activated incremental search by typing "C-s", but have yet to type anything else. Point is still where it was to start off with. Let's type in an "a"
A|fter drinking, King Arthur lowered his visor. He searched around and saw Puck, Ariel, and the noders ariels and arieh.
I-search: a
The first match for "a" is the first letter of "after", so point advances there. After we type in "r", "after" no longer matches -- so we go to the next match:
After drinking, King Ar|thur lowered his visor. He searched around and saw Puck, Ariel, and the noders ariels and arieh.
I-search: ar
Typing in "i" brings us further ahead, and then "e" keeps us on the first "Ariel", just advancing point by one character:
After drinking, King Arthur lowered his visor. He searched around and saw Puck, Arie|l, and the noders ariels and arieh.
I-search: arie
Ack! That's not "arieh". Rather than type in "h", let's hit C-s again, to advance to the next match. Typing C-s advances to the next match. If we're already at the last match, C-s fails, but searching continues; hitting C-s again will wrap around to the beginning, and search from there. But we just hit C-s once:
After drinking, King Arthur lowered his visor. He searched around and saw Puck, Ariel, and the noders arie|ls and arieh.
I-search: arie
If we hit C-s yet again, we'd get to "arieh", where we wanted to go, but typing "h" does the same (almost -- it also advances another character):
After drinking, King Arthur lowered his visor. He searched around and saw Puck, Ariel, and the noders ariels and arieh|.
I-search: arieh
Found!

Now let's back out. Deleting a character undoes one action: You can undo characters, C-s, or any other incremental search action (see below).

After drinking, King Arthur lowered his visor. He searched around and saw Puck, Ariel, and the noders arie|ls and arieh.
I-search: arie
Deleting the next character will "delete" the C-s we typed, so we'll still be searching for "arie", but point will be where it was earlier. Finally, typing "g" will fail the search:
After drinking, King Arthur lowered his visor. He searched around and saw Puck, Arie|l, and the noders ariels and arieh.
I-search (Failed): arieg
Further typing will keep on failing, of course, so point stays where it is. If we delete the "g" we're still stuck there, but the I-search is no longer "failed", and can continue. In short, it does the right thing.

More keys

Apart from C-s (find next), several other keys help search:

  • C-r: Search backwards. This can be used to initiate a new search, or -- while searching -- to search for the same string, in the other direction.
  • C-w: Copies the remainder of the current word into the search string. E.g., when standing at "Ar|thur" above, hitting C-w will change the search string to "arthur", just as typing t h u r would have done.
  • C-g: Abort the search. Point returns to its original location, and the current search string is forgotten.
  • Enter: End the search. Point stays where it was; the current search string is remembered.
  • Any other command keys: End the search, and process those command keys. The current search string is remembered.
  • C-s with an empty search string: When starting to search, you're searching for nothing. The next match would just be the next character, which is useless. Instead, hitting C-s will insert the last search string to be the current search string. So one way to perform editing tasks on all occurrences of a string is to hit C-s to find the first, edit at that location, and then quickly hit C-s twice to advance to the next matching location.
  • C-y yanks from the kill ring into the search string; this will only make sense if you're an Emacs junkie.
  • M-C-s, M-C-r: Incremental search, for a regexp. This behaves as you'd expect -- if you ever expected to search incrementally for a regexp. Some partial inputs (like "[a-z") are invalid regexps; these are flagged, and searching pauses until you finish typing in a valid regexp. Some partial inputs (like entering a "?" to get "c?") can take your search string back, since they match less.

Have fun finding things!


Many thanks to arieh, who agreed to have his name dragged through an Emacs text buffer so that incremental search could be praised.

Log in or registerto write something here or to contact authors.