Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

emacs: interactively search open buffers

Tags:

Is there a way to search all the open buffers for a particular pattern?

C-s interactively searches current buffer. Similarly, is there something that searches all the open buffers?

I know I can use "occur", but "Occur" brings a new buffer and changes/messes with the buffer organization.

like image 820
iobelix Avatar asked Apr 14 '10 21:04

iobelix


People also ask

How many buffer open in Emacs at a time?

At any time, one and only one buffer is selected. It is also called the current buffer. Often we say that a command operates on "the buffer" as if there were only one; but really this means that the command operates on the selected buffer (most commands do).

How do I switch between buffers in Emacs?

For conveniently switching between a few buffers, use the commands C-x <LEFT> and C-x <RIGHT> . C-x <RIGHT> ( previous-buffer ) selects the previous buffer (following the order of most recent selection in the current frame), while C-x <LEFT> ( next-buffer ) moves through buffers in the reverse direction.

How do I use Grep in Emacs?

To run grep , type M-x grep , then enter a command line that specifies how to run grep . Use the same arguments you would give grep when running it normally: a grep -style regexp (usually in single-quotes to quote the shell's special characters) followed by file names, which may use wildcards.

How many buffers can you have in Emacs?

This is because Emacs tracks buffer positions using that data type. For typical 64-bit machines, this maximum buffer size is 2^{61} - 2 bytes, or about 2 EiB. For typical 32-bit machines, the maximum is usually 2^{29} - 2 bytes, or about 512 MiB.


2 Answers

The built-in multi-occur-in-matching-buffers hasn't been mentioned. I use a modified version of this (because I invariably want to search all buffers, and specifying a buffer name pattern each time is annoying).

(defun my-multi-occur-in-matching-buffers (regexp &optional allbufs)   "Show lines matching REGEXP in all file-visiting buffers.  Given a prefix argument, search in ALL buffers."   (interactive (occur-read-primary-args))   (multi-occur-in-matching-buffers "." regexp allbufs))  (global-set-key (kbd "M-s /") 'my-multi-occur-in-matching-buffers) 

To invert the behaviour of the prefix argument so that the default behaviour is to search all buffers, change the call to:

(multi-occur-in-matching-buffers "." regexp (not allbufs)) 

(and, of course, update the docstring accordingly.)

like image 110
phils Avatar answered Oct 01 '22 04:10

phils


I've fixed the TODO:

;; I know that string is in my Emacs somewhere! (require 'cl) (defcustom search-all-buffers-ignored-files (list (rx-to-string '(and bos (or ".bash_history" "TAGS") eos)))   "Files to ignore when searching buffers via \\[search-all-buffers]."   :type 'editable-list)  (require 'grep) (defun search-all-buffers (regexp prefix)   "Searches file-visiting buffers for occurence of REGEXP.  With prefix > 1 (i.e., if you type C-u \\[search-all-buffers]), searches all buffers."   (interactive (list (grep-read-regexp)                      current-prefix-arg))   (message "Regexp is %s; prefix is %s" regexp prefix)   (multi-occur    (if (member prefix '(4 (4)))        (buffer-list)      (remove-if       (lambda (b) (some (lambda (rx) (string-match rx  (file-name-nondirectory (buffer-file-name b)))) search-all-buffers-ignored-files))       (remove-if-not 'buffer-file-name (buffer-list))))     regexp))  (global-set-key [f7] 'search-all-buffers) 
like image 34
offby1 Avatar answered Oct 01 '22 02:10

offby1