[Chapter 32] 32.8 Rational Searches
Author: Jerry Peek, Tim O'Reilly & Mike Loukides
Date: 1998-08-04T21:46:37Z

#

| Chapter 32 GNU Emacs |

## 32.8 Rational Searches

Emacs has, oh, a hundred or so different search commands. (Well, really, 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, [3] regular expression searches, and so on.

> [3] These are especially nice because they can search for phrases that cross linebreaks; 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 (43.12), 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. And it lacks one very important feature: you can't search for the same string repeatedly. That is, you can't say "OK, you found the right sequence of letters; give me the next occurrence"; you have to retype your search string every time.

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 (4.4), 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 mini-buffer. 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 in order 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 like 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

32.7 Mike's Favorite Time Savers |  | 32.9 Unset PWD Before Using Emacs