问题
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.
回答1:
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))))
回答2:
Files
This can be done using package noccur which can be installed from MELPA.
It provides two functions:
noccur-dired
that will performmulti-occur
on dired marked filesnoccur-project
that will performmulti-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.
回答3:
You may 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 dired buffer with M-x dired-do-multi-occur
or bind it to a key of your liking.
Warning: all marked files will be opened by emacs.
回答4:
In Icicles, use M-s M-s m
(command icicle-search-dired-marked-recursive
) to search the marked files in the current Dired buffer and in all marked subdirs, recursively.
Similarly, in your bookmark-list display the same key, M-s M-s m
, searches the targets of all marked bookmarks. And similarly for Ibuffer and Buffer Menu: M-s M-s m
searches the marked buffers.
This is Icicles search, which is a different kind of incremental search (and on-demand replace). You can confine searching within particular contexts (defined by a regexp). The search hits are updated incrementally as you change your search pattern. You can combine multiple search patterns, to refine your search progressively. You can cycle through any set of search hits or access them directly. You can change cycle orders --- you are not limited to buffer-occurrence order.
- http://www.emacswiki.org/emacs/Icicles_-_Search_Commands%2c_Overview
回答5:
Let me improve on the answer of mk1 since I think it is the best one so far. This keeps the same history of previous searches of occur and allows for the optional argument for displaying more lines after or before the match (using C-u followed by a number before calling the function), just as in standard occur.
(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))
回答6:
Wrt Dired:
Not sure whether you are asking (a) to regexp-search the files marked in Dired or (b) to mark files (in Dired) whose contents match a regexp. - or (c) something else.
You can do the former (search marked files) using
A
(orM-S a C-M-s
for incremental regexp search). And this answer lets you search all files marked here and in marked subdirs (recursively).You can do the latter (mark the files whose contents match) using
%q
(dired-mark-files-containing-regexp
).
来源:https://stackoverflow.com/questions/16661371/emacs-is-it-possible-to-list-all-matching-lines-for-a-certain-query-string-for