Other Search-and-Loop Commands
Here are some other commands that find matches for regular expressions. They all ignore case in matching, if the pattern contains no upper-case letters and case-fold-search is non-nil. Aside from multi-occur and multi-occur-in-matching-buffers, which always search the whole buffer, all of the commands operate on the text from point to the end of the buffer, or on the region if it is active.
- M-x multi-isearch-buffers
- Prompt for one or more buffer names, ending with
RET; then, begin a multi-buffer incremental search in those buffers. (If the search fails in one buffer, the nextC-stries searching the next specified buffer, and so forth.) With a prefix argument, prompt for a regexp and begin a multi-buffer incremental search in buffers matching that regexp. - M-x multi-isearch-buffers-regexp
- This command is just like
multi-isearch-buffers, except it performs an incremental regexp search. - M-x multi-isearch-files
- Prompt for one or more file names, ending with
RET; then, begin a multi-file incremental search in those files. (If the search fails in one file, the nextC-stries searching the next specified file, and so forth.) With a prefix argument, prompt for a regexp and begin a multi-file incremental search in files matching that regexp. - M-x multi-isearch-files-regexp
- This command is just like
multi-isearch-files, except it performs an incremental regexp search. In some modes that set the buffer-local variablemulti-isearch-next-buffer-function(e.g., in Change Log mode) a multi-file incremental search is activated automatically. @c Too long. @c @cindex list-matching-lines-current-line-face (face name) - M-x occur, M-s o
- Prompt for a regexp, and display a list showing each line in the buffer that contains a match for it. If you type
M-nat the prompt, you can reuse search strings from previous incremental searches. The text that matched is highlighted using thematchface. A numeric argument n specifies that n lines of context are to be displayed before and after each matching line. The default number of context lines is specified by the variablelist-matching-lines-default-context-lines. Whenlist-matching-lines-jump-to-current-lineis non-nilthe current line is shown highlighted with facelist-matching-lines-current-line-faceand the point is set at the first match after such line. You can also runM-s owhen an incremental search is active; this uses the current search string. Note that matches for the regexp you type are extended to include complete lines, and a match that starts before the previous match ends is not considered a match. The*Occur*buffer uses the Occur mode as its major mode. You can use thenandpkeys to move to the next or previous match; with prefix numeric argument, these commands move that many matches. Digit keys are bound todigit-argument, so5 nmoves to the fifth next match (you don't have to typeC-u).SPCandDELscroll the*Occur*buffer up and down. Clicking on a match or moving point there and typingRETvisits the corresponding position in the original buffer that was searched.oandC-odisplay the match in another window;C-odoes not select that window. Alternatively, you can use theM-g M-n(next-error) command to visit the occurrences one by one (Compilation Mode). Finally,qquits the window showing the*Occur*buffer and buries the buffer. Typingein the*Occur*buffer makes the buffer writable and enters the Occur Edit mode, in which you can edit the matching lines and have those edits reflected in the text in the originating buffer. TypeC-c C-cto leave the Occur Edit mode and return to the Occur mode. The commandM-x list-matching-linesis a synonym forM-x occur. - M-x multi-occur
- This command is just like
occur, except it is able to search through multiple buffers. It asks you to specify the buffer names one by one. - M-x multi-occur-in-matching-buffers
- This command is similar to
multi-occur, except the buffers to search are specified by a regular expression that matches visited file names. With a prefix argument, it uses the regular expression to match buffer names instead. - M-x how-many
- Prompt for a regexp, and print the number of matches for it in the buffer after point. If the region is active, this operates on the region instead.
- M-x flush-lines
- Prompt for a regexp, and delete each line that contains a match for it, operating on the text after point. When the command finishes, it prints the number of deleted matching lines. This command deletes the current line if it contains a match starting after point. If the region is active, it operates on the region instead; if a line partially contained in the region contains a match entirely contained in the region, it is deleted. If a match is split across lines,
flush-linesdeletes all those lines. It deletes the lines before starting to look for the next match; hence, it ignores a match starting on the same line at which another match ended. - M-x keep-lines
- Prompt for a regexp, and delete each line that does not contain a match for it, operating on the text after point. If point is not at the beginning of a line, this command always keeps the current line. If the region is active, the command operates on the region instead; it never deletes lines that are only partially contained in the region (a newline that ends a line counts as part of that line). If a match is split across lines, this command keeps all those lines.
- M-x kill-matching-lines
- Like
flush-lines, but also add the matching lines to the kill ring. The command adds the matching lines to the kill ring as a single string, including the newlines that separated the lines. - M-x copy-matching-lines
- Like
kill-matching-lines, but the matching lines are not removed from the buffer.