7

I want to have a similar tool in Emacs as the following in Vim

 :g/search/

to get a list of matches.

How can you get a list of matches in Emacs?

Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697

3 Answers3

16

M-x occur?

From the manual:

M-x occur

Prompt for a regexp, and display a list showing each line in the buffer that contains a match for it. The text that matched is highlighted using the match face. 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 default number of context lines is specified by the variable list-matching-lines-default-context-lines.

In the *Occur* buffer, you can click on each entry, or move point there and type RET, to visit the corresponding position in the buffer that was searched. o and C-o display the match in another window; C-o does not select it. Alternatively, you can use the C-x ` (next-error) command to visit the occurrences one by one (see Compilation Mode).

Typing e in the *Occur* buffer switches to Occur Edit mode, in which edits made to the entries are also applied to the text in the originating buffer. Type C-c C-c to return to Occur mode.

The command M-x list-matching-lines is a synonym for M-x occur.

Community
  • 1
  • 1
LB40
  • 12,041
  • 17
  • 72
  • 107
5

In addition to M-x occurr check also M-x grep. This works in several files at once.

dfa
  • 114,442
  • 31
  • 189
  • 228
4

My usual workflow is not to get a list and choose (don't know how to do that), but to use the incremental search:

C-s <search target>

gets the fist match after the point. If you don't like it another C-s gets the next one. Continue until you're happy (you'll need an extra C-s to wrap around from the and of the buffer). New enough emacsen can also highlight all the matches that are visible.

As noted in the comments by Török Gábor, this is the typical keybinding of isearch-forward. In the event that your bindings are different, you need to modify the prescribed procedure.

dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234