Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs: Is it possible to list all matching lines for a certain query string for marked files in dired?

I found out M-x occur the other day.
(How to achieve code folding effects in Emacs?)

I wonder if I could list all matching lines in multiple files(or buffers) preferably marked in dired mode.

like image 944
eugene Avatar asked Jan 13 '23 08:01

eugene


2 Answers

Files

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

It provides two functions:

  • noccur-dired that will perform multi-occur on dired marked files
  • noccur-project that will perform multi-occur on all files in current project. This is recursive.

From documentation a typical usage is: M-x noccur-project RET foo RET
The occur buffer's content can then be edited with occur-edit-mode (bound to e). To save changes in all modified buffer and go back to occur-mode press C-c C-c.

Buffers

This can be done using the built-in ibuffer. Mark buffers with m key, then key O to launch ibuffer-do-occur on 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 that will perform multi-occur on buffers matching a regexp. Typical usage is M-x multi-occur-in-matching-buffers RET ext$ RET regexp RET where ext$ is regexp for buffers already opened in Emacs, and regexp is what to match.

like image 155
thdox Avatar answered Jan 19 '23 00:01

thdox


M-x multi-occur

M-x multi-occur-in-matching-buffers

and also:

M-x multi-occur-in-this-mode

(defun get-buffers-matching-mode (mode)
  "Returns a list of buffers where their major-mode is equal to MODE"
  (let ((buffer-mode-matches '()))
   (dolist (buf (buffer-list))
     (with-current-buffer buf
       (if (eq mode major-mode)
           (add-to-list 'buffer-mode-matches buf))))
   buffer-mode-matches))

    (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))))
like image 21
aartist Avatar answered Jan 19 '23 00:01

aartist