Sams Teach Yourself Emacs in 24 Hours |  | ContentsIndex
Hour 7: Searching for Text in a Buffer |  | Previous HourNext Hour
Sections in this Hour: |  |

## Searching for Words

If you are looking at a formatted version of your document (that is, a document formatted with HTML or LaTeX), you will not know where the line breaks are in your input file. This makes an ordinary incremental search inappropriate, because searching for two words with a space in between might not match the occurrences you are looking for, because the occurrences might be split with a line break instead of a space in your file. For that purpose Emacs has a nonincremental search mode called word search, which interprets a space in the search string as either a space, a tabulator, or a newline. Furthermore, it forces the search string to match whole words, that is the text the will only match the word the, not there or then.

To search using word search, you must press C-s RET C-w for searching forward and C-r RET C-w to search backward. These bindings exist only for historical reasons, and if you use them often, it might be a very good idea to bind them to, for example, C-S-s and C-S-r (that is, Control-Shift and the letter s or r, which are unbound per default in Emacs). The functions invoked are called word-search-forward and word-search- backward.

The main drawback of using word search is that it is nonincremental. If you are willing to resign from matching at word boundaries, you can use regular-incremental-search to obtain the capability to interpret line breaks as ordinary spaces in incremental search. To do this, you must insert the following line into your .emacs file:

    (setq search-whitespace-regexp "[ \t\r\n]+")

Instead of using C-s (isearch-forward) and C-r (isearch-backward), you must now use M-C-s (isearch-forward-regexp) and M-C-r (isearch-backward-regexp) instead. These functions are regular expression searches; therefore, characters such as ., ?, [, ], and * have special meanings and you must not use them in your search string. (If you really need to, you must prefix them with a backslash--for example, \?).

For more information on regular expression searches, see Hour 9.

Sams Teach Yourself Emacs in 24 Hours |  | ContentsIndex
Hour 7: Searching for Text in a Buffer |  | Previous HourNext Hour
Sections in this Hour: |  |

© Copyright Macmillan USA. All rights reserved.