Emacs: Is it possible to list all the relevant lines for a specific query string for marked files in dired?

I found that Mx is happening the other day.
( How to achieve the effect of folding code in Emacs? )

I wonder if I can list all the relevant lines in several files (or buffers), preferably marked in standby mode.

+3
search emacs dired
source share
6 answers
Mx multi-occur Mx multi-occur-in-matching-buffers 

and:

 Mx multi-occur-in-this-mode (defun multi-occur-in-this-mode () "Show all lines matching REGEXP in buffers with this major mode." (interactive) (multi-occur (get-buffers-matching-mode major-mode) (car (occur-read-primary-args)))) 
+2
source share

You can try

 (defun dired-do-multi-occur (regexp) "Run `multi-occur' with REGEXP on all marked files." (interactive (list (read-regexp "Regexp: "))) (multi-occur (mapcar 'find-file-noselect (dired-get-marked-files)) regexp)) 

Run it in a fading buffer using Mx dired-do-multi-occur or bind it to a key to your liking.

Warning: all marked files will be opened by emacs.

+1
source share

In Icicles, use Ms Ms m ( icicle-search-dired-marked-recursive command) to search for marked files in the current Dired buffer and in all marked subdirs, recursively.

Similarly, your bookmark list displays the same key, Ms Ms m , searching for targets for all bookmarks that are marked. And similarly for Ibuffer and Buffer Menu: Ms Ms m searches for marked buffers.

This is Icicles search, which is another type of incremental search (and on-demand replacement). You can limit the search in certain contexts (defined by regular expression). As you change your search template, search queries are updated in stages. You can combine several search templates to refine your search gradually. You can cycle through any set of search queries or directly access them. You can change the order of the cycles - you are not limited by the order in which the buffer is filled.

+1
source share

Let me improve mk1's answer, as I think this is the best so far. This preserves the same history of previous searches and allows you to use an additional argument to display more lines after or before the match (using Cu, followed by a number before calling the function), as in the standard.

 (defun dired-do-multi-occur (regexp &optional nlines) "Run `multi-occur' with REGEXP on all dired marked files." (interactive (occur-read-primary-args)) (multi-occur (mapcar 'find-file-noselect (dired-get-marked-files)) regexp nlines)) 
+1
source share

Wrt Dired:

Not sure if you are requesting (a) a regular expression to search for files marked in Dired or (b) to mark files (in Dired) whose contents match the regular expression. - or (c) something else.

  • You can do the first (search for marked files) using A (or MS a CMs for incremental regular expression searches). And this answer allows you to search for all the files marked here and in the marked sub-directors (recursively).

  • You can do the latter (mark the files whose contents match) using %q ( dired-mark-files-containing-regexp ).

+1
source share

Files

This can be done using the noccur package, which can be installed from MELPA .

It provides two functions:

  • noccur-dired , which will perform multi-occur in tagged files
  • noccur-project , which will execute multi-occur in all files in the current project . This is recursive.

Typical usage from documentation: Mx noccur-project RET foo RET
Then, the contents of the buffer can be edited using the-edit-mode method (associated with e). To save changes to all modified buffers and return to click mode, press Cc Cc .

Buffers

This can be done using the built-in ibuffer . Mark the buffers with the m key, then press the O key to run ibuffer-do-occur at the marked buffers . I personally activate ibuffer using (defalias 'list-buffers 'ibuffer) in my .emacs .

You can also use the built-in multi-occur-in-matching-buffers , which will execute multi-occur in buffers matching the regular expression . A typical use is Mx multi-occur-in-matching-buffers RET ext$ RET regexp RET , where ext$ is a regular expression for buffers already open in Emacs, and regexp is what needs to be matched.

+1
source share

All Articles