Is there a package which can search for multiple patterns in files and then display the matches highlighted?

You can do this using Icicles.

Use Icicles search.

Since you don't want line-oriented search (like grep or occur) you want to search a single context - the whole file. And you want to search multiple files.

Use icicle-search-file, bound in Icicle mode to M-s M-s f.

  1. It prompts you for a regexp that defines the contexts to search. You enter \(.\|\n\)+, which means any number of any characters, i.e., the whole file.

  2. It then prompts you for the subgroup of that regexp to use, to define the search context. You enter 0, as you want the whole regexp to define the context (which is the whole file).

  3. It then prompts you with Choose file to search (RET' when done):`.

    This means enter a pattern to match one or more files, then C-RET, then another such pattern, then C-RET,...then another pattern, then RET.

    You can enter just a single pattern if you like, then RET. (RET ends input.) But this is a multi-command, which means that you can use C-RET to act on multiple completion candidates, i.e., you can enter multiple inputs.

    Each pattern you enter can be literal or a regexp (or a fuzzy-match pattern).

What you've now defined, and started searching, is this: a set of files to search and a pattern defining the search context: the whole file.

S-TAB at any time from now on shows you the matches, across all files. But don't be in a hurry to hit S-TAB right away, because all lines of all files match an empty search pattern.

You start typing a search pattern - another regexp (or literal text): cat.

To enter another search pattern, to be also matched, hit C-SPC. You now see, in buffer *Completions*, the matches, across all files you're searching, of cat. And each search hit is labeled with its file name. (And that file name can also be searched on, so you can, for example, interactively narrow to a subset of the files.)

Now type another search pattern: dog. The search hits are updated (narrowed), to show only hits with both cat and dog - in either order.

Now narrow to a third pattern: C-SPC mouse. Now you've got all hits with cat, dog, and mouse, in any order.

You can navigate among the search hits in multiple ways, including cycling (e.g. C-<down>), temporarily)entering more pattern text to narrow the choices (e.g. cycling among fewer), and cycling through candidates in *Completions* without visiting each one (e.g. <down>) - then visiting the "current" one (C-RET).

/r/emacs Thread