Lets say I have 10 buffers open, and I want to find all occurrences of blah
in all 10 buffers. How would I do this?
9 Answers
Sticking to built-in Emacs commands, you can use multi-occur
or multi-occur-in-matching-buffers
(M-x multi-occur- TAB RET
). multi-occur
gives you fine control by prompting for each buffer to use, but it's tedious. multi-occur-in-matching-buffers
lets you enter a regexp to match file names (it only searches in buffers that are visiting files); enter .
as the file regexp to search in all buffers that are visiting a file.
There are also functions for incremental search across a list of buffers or files, but they're tedious like multi-occur
as they require listing the buffers or files one by one.
To search in all the files in a directory, or files matching certain patterns, whether they're open or not, you can use grep
. There are variants rgrep
and grep-find
to search subdirectories recursively.
-
9+1 for mentioning that
.
is the regular expression to match all files.– user2699Jan 27, 2017 at 15:30
swiper-all
from the swiper package provides a tool for doing this. It's even incremental, so it updates as you type.
-
1
This can be done with helm via the command helm-multi-occur
. You can install helm from melpa and then call the command as follows:
M-x helm-multi-occur RET M-a RET
This will open helm-multi-occur
and then select all buffers. Instead of using M-a
to mark all buffers, you can optionally select the buffers of interest one at a time. Navigate up and down the list with C-n
and C-p
and mark buffers with C-SPC
. Once all of the buffers are marked, press RET
to accept the marked buffers and start searching.
If you add the following to your init file:
(require 'helm-regexp)
(eval-after-load "helm-regexp"
'(setq helm-source-moccur
(helm-make-source "Moccur" 'helm-source-multi-occur :follow 1)))
;; (source: http://stackoverflow.com/q/14726601)
(defun my-helm-multi-occur-buffers ()
"multi-occur in all buffers backed by files."
(interactive)
(helm-multi-occur
(delq nil
(mapcar (lambda (b)
(when (buffer-file-name b) (buffer-name b)))
(buffer-list)))))
You can then bind the command my-helm-multi-occur-buffers
to do a multi-occur in all buffers that are backed by files.
-
3You might also want to look at
helm-swoop
package in MELPA. It provides this functionality and allows quick editing of the results as well.– VamsiOct 6, 2014 at 23:25
In addition to what others have mentioned - here are two other resources for this.
M-x multi-isearch-buffers
orM-x multi-isearch-buffers-regexp
(vanilla Emacs) - Incremental search across multiple buffers.If you use Icicles then you can use
icicle-search
(oricicle-occur
, or any number of othericicle-search
specializations) to search across multiple files or buffers.
(In addition to searching and replacing across arbitrary buffers, there are of course commands that do that across multiple files. Others have mentioned grep
, for instance. And there is of course A
in Dired. But you did not ask about searching files.)
You can use ibuffer
mode to mark the buffers you would like to search. Call ibuffer-do-isearch
(bound to M-s a C-s
by default) to search for text, or ibuffer-do-isearch-regexp
if you need to search for a regular expression.
If you aren't familiar with ibuffer
, fire it up and take a look at the Mark menu to see all the ways you can choose a set of buffers on which to operate.
Here is another way with helm: M-x helm-mini M-a C-s blah
. After invoking helm-mini
, M-a
will mark all opening buffers then C-s blah
will do multi occur on marked buffers.
In vanilla Emacs this exact sequence will find "blah" in any buffer:
C-u M-x multi-occur-in-matching-buffers RET . RET blah RET
To give it a better name and save a few keystrokes each time you use it, you can add a function like this to your config:
(defun search-all-buffers (regexp)
(interactive "sRegex: ")
(multi-occur-in-matching-buffers "." regexp t))
-
2
-
@JeanPierre No, Gilles answer is missing a key part and has a bunch of irrelevant parts. This is the correct answer to the question that was asked. It finds "blah" in all open buffers regardless of wether they are attached to a file or not. Jan 27, 2020 at 14:40
I love find-name-dired
for searching multiple files in emacs. It's great if you only want to search your code files.
M-x find-name-dired
Find-name (directory): ~/code <path at the top of your project> [RET]
Find-name (filename wildcard): *.[ch] <wildcard to select the files to include> [RET]
It opens a buffer called *Find*
. Put your cursor in the top left corner and hit m
. That will mark all the files listed. You can hit m
by each file you want.
Then there are two very useful functions.
'A' will give you a regex prompt that will pull up a list of matches.
'Q' is a global search and replace.
'h' will give you the help doc.
Also, a solution using helm-mode is with helm-swoop
.
Use
M-x helm-swoop RET
then, for searching in all open buffers, type
M-i
then, just type blah
to search it in all buffers. On a different window will show all occurrences.
Alternatively, you can just type
M-x helm-multi-swoop
then, mark all buffers you wish to search with M-SPC
or C-SPC
, press RET
and search for blah
.