next up previous contents index
Next: What's Really Going On Up: Editing files with Emacs Previous: CuttingPasting, Killing and

Searching and Replacing

There are several ways to search for text in Emacs. Many of them are rather complex, and not worth going into here. The easiest and most entertaining way is to use isearch . ``Isearch'' stands for ``incremental search''. Suppose you want to search for the string ``gadfly'' in the following buffer:

I was growing afraid that we would run out of gasoline, when my passenger exclaimed ``Gadzooks! There's a gadfly in here!''.

You would move to the beginning of the buffer, or at least to some point that you know is before the first occurence of the goal word, ``gadfly'', and type C-s. That puts you in isearch mode. Now start typing the word you are searching for, ``gadfly''. But as soon as you type the ``g'', you see that Emacs has jumped you to the first occurence of ``g'' in the buffer. If the above quote is the entire contents of the buffer, then that would be the first ``g'' of the word ``growing''. Now type the ``a'' of ``gadfly'', and Emacs leaps over to ``gasoline'', which contains the first occurence of a ``ga''. The ``d'' gets you to gadzooks, and finally, ``f'' gets you to ``gadfly'', without your having had to type the entire word.

What you are doing in an isearch is defining a string to search for. Each time you add a character to the end of the string, the number of matches is reduced, until eventually you have entered enough to define the string uniquely. Once you have found the match you are looking for, you can exit the search with tex2html_wrap8352 or any of the normal movement commands. If you think the string you're looking for is behind you in the buffer, then you should use C-r, which does an isearch backwards.

If you encounter a match, but it's not the one you were looking for, then hit C-s again while still in the search. This will move you forward to the next complete match, each time you hit it. If there is no next match, it will say that the search failed, but if you press C-s again at that point, the search will wrap around from the beginning of the buffer. The reverse holds true for C-r -- it wraps around the end of the buffer.

Try bringing up a buffer of plain English text and doing and isearch for the string ``the''. First you'd type in as much as you wanted, then use repeated C-s's to go to all instances of it. Notice that it will match words like ``them'' as well, since that also contains the substring ``the''. To search only for ``the'', you'd have to do add a space to the end of your search string. You can add new characters to the string at any point in the search, even after you've hit C-s repeatedly to find the next matches. You can also use tex2html_wrap8378 or tex2html_wrap8380 to remove characters from the search string at any point in the search, and hitting tex2html_wrap8352 exits the search, leaving you at the last match.

Emacs also allows you to replace all instances of a string with some new string--this is known as query-replace. To invoke it, type query-replace and hit tex2html_wrap8352 . Completion is done on the command name, so once you have typed ``query-re'', you can just hit tex2html_wrap8288 to finish it. Say you wish to replace all instances of ``gadfly'' with ``housefly''. At the ``Query replace: '' prompt, type ``gadfly'', and hit tex2html_wrap8352 . Then you will be prompted again, and you should enter ``housefly''. Emacs will then step through the buffer, stopping at every instance of the word ``gadfly'', and asking if you want to replace it. Just hit ``y'' or ``n'' at each instance, for ``Yes'' or ``No'', until it finishes. If this doesn't make sense as you read it, then try it out.  

next up previous contents index
Next: What's Really Going On Up: Editing files with Emacs Previous: CuttingPasting, Killing and

Converted on:
Mon Apr 1 08:59:56 EST 1996