GNU Emacs Manual. Node: Other Repeating Search

PREV Replace UP Search next

10.8: Other Search-and-Loop Commands

Here are some other commands that find matches for a regular expression. They all operate from point to the end of the buffer, and all ignore case in matching, if the pattern contains no upper-case letters and case-fold-search is non-nil.

M-x occur RET regexp RET

Display a list showing each line in the buffer that contains a match for regexp. A numeric argument specifies the number of context lines to print before and after each matching line; the default is none. To limit the search to part of the buffer, narrow to that part (see Narrowing).

The buffer `*Occur*' containing the output serves as a menu for finding the occurrences in their original context. Click Mouse-2 on an occurrence listed in `*Occur*', or position point there and type RET; this switches to the buffer that was searched and moves point to the original of the chosen occurrence.

M-x list-matching-lines

Synonym for M-x occur.

M-x count-matches RET regexp RET

Print the number of matches for regexp after point.

M-x flush-lines RET regexp RET

Delete each line that follows point and contains a match for regexp.

M-x keep-lines RET regexp RET

Delete each line that follows point and does not contain a match for regexp.

In addition, you can use grep from Emacs to search a collection of files for matches for a regular expression, then visit the matches either sequentially or in arbitrary order. See Grep Searching. PREV Replace UP Search next