## 19.8. Rational Searches

Emacs has, oh, a hundred or so different search commands. (Well, the number's probably more like 32, but who's counting?) There are searches of absolutely every flavor you could ever imagine: incremental searches, word searches,[54] regular-expression searches, and so on.

> [54]These are especially nice because they can search for phrases that cross line breaks; most searches assume that all the text you want will all be on the same line. However, you can only search for whole words, and if you use troff or TEX, Emacs may be confused by your "markup."

However, when it comes to your plain old garden-variety search, Emacs is strangely deficient. There is a simple search that just looks for some arbitrary sequence of characters, but it's rather well hidden. In addition, it lacks one very important feature: you can't search for the same string repeatedly. That is, you can't say "Okay, you found the right sequence of letters; give me the next occurrence"; you have to retype your search string every time.

Go to http://examples.oreilly.com/upt3 for more information on: search.el

I thought this was an incredible pain until a friend of mine wrote a special search command. It's in the file search.el. Just stick this into your directory for Emacs hacks (Section 19.12), and add something like the following to your .emacs file:

>     ;; real searches, courtesy of Chris Genly
>     ;; substitute your own Emacs hack directory for /home/los/mikel/emacs
>      (load-file "/home/los/mikel/emacs/search.el")

Now you can type CTRL-s to search forward and CTRL-r to search back. Emacs will prompt you for a search string and start searching when you press RETURN. Typing another CTRL-s or CTRL-r repeats your previous search. When you try this, you'll see one other useful feature: unlike the other Emacs searches, this kind of search displays the "default" (i.e., most recent) search string in the minibuffer. It's exactly the kind of search I want.

It's conceivable that you'll occasionally want incremental searches. You'll have to "rebind" them, though, to use them conveniently. Here are the key bindings that I use:

>     ;; rebind incremental search as ESC-s and ESC-r
>      (define-key global-map "\M-s" 'isearch-forward)
>      (define-key global-map "\M-r" 'isearch-backward)
>      ;; have to rebind ESC s separately for text-mode. It's normally
>      ;; bound to 'center-line'.
>      (define-key text-mode-map "\M-s" 'isearch-forward)

That is, ESC-s and ESC-r now give you forward and reverse incremental searches. And once you've started an incremental search, CTRL-s and CTRL-r still repeat the previous incremental search, just as they're supposed to.

Of course, now you'll have to rebind the "center-line" command if you're fond of it. In my opinion, it's not worth the trouble. The game of "musical key-bindings" stops here.

-- ML

Copyright © 2003 O'Reilly & Associates. All rights reserved.