36

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?

31

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.

  • 5
    +1 for mentioning that . is the regular expression to match all files. – user2699 Jan 27 '17 at 15:30
12

In addition to what others have mentioned - here are two other resources for this.

  1. M-x multi-isearch-buffers or M-x multi-isearch-buffers-regexp (vanilla Emacs) - Incremental search across multiple buffers.

  2. If you use Icicles then you can use icicle-search (or icicle-occur, or any number of other icicle-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.)

10

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.

  • 3
    You might also want to look at helm-swoop package in MELPA. It provides this functionality and allows quick editing of the results as well. – Vamsi Oct 6 '14 at 23:25
9

swiper-all from the swiper package provides a tool for doing this. It's even incremental, so it updates as you type.

enter image description here

  • this is great ! nice find – abourget Mar 16 '18 at 3:04
6

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.

1

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.