Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 7: Searching for Text in a Buffer

Previous HourNext Hour

Sections in this Hour:

 

Listing Lines that Match a Pattern


Sometimes it is easier to find some text, if you can get a list of all lines in the buffer that contain the text you are searching for, instead of having to travel to each and every match with an incremental search. Emacs has a function, which is not bound to any key, called occur. To use it, press M-x and type occur and the text which you are searching for. When you finish that, each match is listed in a buffer called *OCCUR*, with one match at every line. If you give this function a numerical prefix (that is, by pressing C-3 before the function) then that many lines of context are shown. In Figure 7.16, an example of the output from occur can be seen.

Figure 7.16
Sample output from C-2 M-x occur RET <h3> RET.

Caution - The text you insert in the minibuffer is interpreted as a regular expression. For simple search strings, this doesn't affect you, but you should be careful when you use symbols such as *, ., and so on.


The screen is split in two--in the upper part your original buffer is shown, and in the lower part, the special occur buffer is shown. If you press Enter or the middle mouse button over one of these matches, Emacs will proceed to this location in your original buffer.

Tip - On the CD-ROM included with this book, a library called sams-lib.el exists in the directory usr/share/emacs-lisp/sams. This lets you edit the matches in the occur buffer (which, with this library, is called *ALL*). To go to a match in the original buffer, press C-c C-c. To use this library, insert the following line in your .emacs file:

(autoload 'all "all" nil t)

Killing Lines that Match or Do Not Match a Pattern

In sams-lib.el, there exist two functions for keeping and killing lines that fulfill a regular expression. These functions are called sams-keep-lines and sams-kill-lines, respectively.

Caution - The functions work from point to the end of the buffer, so if you want to kill or keep lines that match a regular expression in the whole buffer, you need to go to the beginning of the buffer before you invoke the function.


The functions are, in fact, based on built-in Emacs functions that do the actual deleting of lines, but without a security belt.

When you invoke the functions, they first count the number of lines that match your query and ask whether you are certain that you want to delete this many lines in your buffer. This ensures that you do not delete hundreds of lines by accident without noticing. If you confirm to delete them, the actual deletion takes place.

In a situation where your buffer was originally unmodified, Emacs offers to mark it as unmodified after the deletion (that is, without actually saving to disk) and makes it read-only. This makes it possible to delete thousands of lines in a buffer and view it without the risk of you saving it by habit and, thus, losing all the lines.

Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 7: Searching for Text in a Buffer

Previous HourNext Hour

Sections in this Hour: