Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I "M-x replace-string" across all buffers in emacs?

Tags:

How do I "M-x replace-string" across all buffers in emacs?

like image 444
David Zureick-Brown Avatar asked Sep 21 '11 03:09

David Zureick-Brown


People also ask

How do I change all occurrences in Emacs?

When you want to replace every instance of a given string, you can use a simple command that tells Emacs to do just that. Type ESC x replace-string RETURN, then type the search string and press RETURN. Now type the replacement string and press RETURN again.

How do I close all buffers in Emacs?

M-x kill-matching-buffers. Offer to kill all buffers matching a regular expression. C-x k ( kill-buffer ) kills one buffer, whose name you specify in the minibuffer. The default, used if you type just RET in the minibuffer, is to kill the current buffer.


2 Answers

M-x ibuffer RET t U

But you'll probably want to be a bit more restrictive than that, because it will abort if it can't do a replacement -- e.g. encounters a read-only dired buffer containing a matching filename.

C-hm within ibuffer to read the mode help, and learn how to easily mark just the buffers you're interested in.

Edit: a non-regexp version of ibuffer-do-replace-regexp can easily be written by modifying the original definition:

;; defines ibuffer-do-replace-string (define-ibuffer-op replace-string (from-str to-str)   "Perform a `replace-string' in marked buffers."   (:interactive    (let* ((from-str (read-from-minibuffer "Replace string: "))           (to-str (read-from-minibuffer (concat "Replace " from-str                                                 " with: "))))      (list from-str to-str))    :opstring "replaced in"    :complex t    :modifier-p :maybe)   (save-window-excursion     (switch-to-buffer buf)     (save-excursion       (goto-char (point-min))       (let ((case-fold-search ibuffer-case-fold-search))         (while (search-forward from-str nil t)           (replace-match to-str nil t))))     t)) 
like image 192
phils Avatar answered Oct 19 '22 01:10

phils


There are a bunch of different choices, it kind of depends on how you want to do it.

Check out the Emacs Wiki for SearchBuffers. Of interest would be moccur-edit and icicles.

like image 27
Trey Jackson Avatar answered Oct 19 '22 00:10

Trey Jackson