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?
Sticking to built-in Emacs commands, you can use
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
grep-find to search subdirectories recursively.
swiper-all from the swiper package provides a tool for doing this. It's even incremental, so it updates as you type.
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-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.
In addition to what others have mentioned - here are two other resources for this.
M-x multi-isearch-buffers-regexp(vanilla Emacs) - Incremental search across multiple 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.
Also, a solution using helm-mode is with
M-x helm-swoop RET
then, for searching in all open buffers, type
then, just type
blah to search it in all buffers. On a different window will show all occurrences.
Alternatively, you can just type
then, mark all buffers you wish to search with
RET and search for
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))