11.8 Other Search-and-Loop Commands
Here are some other commands that find matches for a regular
expression. They all ignore case in matching, if the pattern contains
no upper-case letters and case-fold-search
is non-nil
.
Aside from occur
, all operate on the text from point to the end
of the buffer, or on the active region in Transient Mark mode.
- M-x occur <RET> regexp <RET>
- Display a list showing each line in the buffer that contains a match
for regexp. To limit the search to part of the buffer, narrow
to that part (see Narrowing). A numeric argument n
specifies that n lines of context are to be displayed before and
after each matching line.
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 how-many <RET> regexp <RET>
- Print the number of matches for regexp that exist in the buffer
after point. In Transient Mark mode, if the region is active, the
command operates on the region instead.
- M-x flush-lines <RET> regexp <RET>
- Delete each line that contains a match for regexp, operating on
the text after point. In Transient Mark mode, if the region is
active, the command operates on the region instead.
- M-x keep-lines <RET> regexp <RET>
- Delete each line that does not contain a match for
regexp, operating on the text after point. In Transient Mark
mode, if the region is active, the command operates on the region
instead.
You can also search multiple files under control of a tags table
(see Tags Search) or through Dired A command
(see Operating on Files), or ask the grep
program to do it
(see Grep Searching).