3

I want to interactively search and replace in all (or, preferably, a regexp subset of) my buffers (not files).

Scenario

  • Being a pack-rat sort of guy, I never delete my buffers until I'm sure I don't need them anymore (e.g. new project), or until the system crashes and I lose them. So I know that all the files I've modified (the targets of the search and replace) are in a buffer somewhere.
  • The files are strewn all over creation so I can't use Dired.
  • There are 10k files in scope, so I'd prefer to avoid file-based operations (though I've already wasted so much time on this I've eclipsed the amount of time I would have saved not using the file-based approach).

What I'm doing now

Right now I'm using multi-occur-in-matching-buffers and repeating a query replace for each matching file.

I'm told Icicles will do this, but reading the manual, I can't make heads or tails of how to do it. (I used M-99 M-x icicle-search; I get prompted for a "context", which I can't find a definition for in the manual and furnishes as a completion a list of previous mini-buffer entries, which doesn't make sense to me.) Icicles is far more than I need, but if I have to use it, I will.

Things I've tried

  • I tried tags-query-replace but it doesn't work; I always get zero matches, even though it ends bringing up a buffer with a match in plain sight.
  • I've read the FAQ (see responses above).

I may end up having to use file-based operations (which are annoying because I have to find and reload each file manually)

Since Icicles supposedly does what I want I thought I'd throw the question out. The other option is to use the Refactor facility in our IDE but that wouldn't be Emacs!! ❤

2
  • 1
    mark your buffers in ibuffer, then use Q or I, after doing the replacement, save unsaved buffers in ibuffer
    – Muihlinn
    May 29, 2020 at 12:13
  • @Muihlinn Good enough, thank you! Feel free to post as an answer; otherwise I will. I hadn't even heard of ibuffer. May 29, 2020 at 14:52

1 Answer 1

4

Ibuffer is what you're looking for.

  • M-x ibuffer
  • Mark your buffers any way you prefer: using m to mark them manually, % g by regexp content-matching, * M to mark by major mode, or whatever other option which suits your fancy -err needs.
  • Use Q to make a regexp search and replace on marked buffers or I for plain search and replace.
  • Do the replaces, then go back to ibuffer
  • S will save the marked buffers.

There are more ways to achieve the same using other popular tools, but ibuffer is powerful, fit your needs, comes with emacs and can do a lot more than this pretty much effortlessly.

To learn more about it, press ? on a ibuffer buffer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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