Comments on “How to edit multiple files on several directories in less than a minute”

Gabriel Saldaña wrote about using search & replace across files in emacs.

Type: M-x dired-do-query-replace-regexp to run the find and replace command. It will prompt you first for the text you want to find, then will prompt you with the text you want to replace it with.

Then Emacs will start the find and replace operation, and will prompt you on every find if you want to replace the text or skip it. To replace, type “y”, to skip to the next find type “n”. To replace all occurrences without asking, type “!”.

I would note that “!” only replaces matches within the same file; every time you start the query-replace in a new file, you must guide Emacs again. This seems like it’s still quite a bit of work, since you have to hit that bang key 239 times.

Now that you’ve made all these changes, you need to save the files. To avoid saving manually all files, you can open ibuffer M-x ibuffer

Which will list all you opened files (called buffers). Now, like in dired, you need to mark the buffers you want to work with. To mark all unsaved files, type “* u” and then type “S” (that’s shift+s, for the capital letter) to save them.

While I recommend ibuffer, there’s no need to bring it in just to save files. Just hit C-x s (save-some-buffers) and hit “!”. Emacs will save every modified buffer for you.

While I understand that Gabriel’s situation is due to less-than-ideal programmers, I would be remiss if I didn’t say don’t do this. Not the query-replace, which is fine, but the structure of the project in which it is being used. It’s copy and paste code reuse, it doesn’t scale, and it’s a bad idea. Avoid.

2008/08/21
Previously On Atomized:

Discussion

You’re right about that, I did have to hit the ! sign all the time. About the C-x s, I didn’t know it saved all modified buffers. But ibuffer is nice to have when editing other buffers that you don’t want to save, and still save lots of other buffers quickly.

About the copy/paste comment, since I don’t know sed or awk or any of those tools to automatically do the job for me, what do you recommend instead?

Gabriel Saldana
2008/08/24

Ibuffer is extremely nice. You should probably use it full-time, that is, make C-x C-b trigger it rather than buffer-menu. I use:

(global-set-key (kbd “C-x C-b”) ‘ibuffer)
(autoload ‘ibuffer “ibuffer” “List buffers.” t)
(add-hook ‘ibuffer-hook ‘(lambda ()
(ibuffer-switch-to-saved-filter-groups “mode”)))

As for the copy/paste comment, you’ve already discovered that what you’re doing doesn’t scale. The problem isn’t lack of tools, but shortcoming of methodology. In a nutshell, you should refactor your code so shared elements are included from a single location, rather than duplicated throughout the entire codebase.

Ian
2008/08/24

Oh, absolutely agree on the scalability issue. But maybe I wasn’t explicit enough on my second paragraph where I stated that the site was being maintained by a pair of . Hence the mess, and the need of doing such a tedious and messy task on those files. The mess is still there because my job was to add the javascript code, not to fix the mess.

Thanks on the ibuffer snippet

Gabriel Saldana
2008/08/25

Yup. That’s why I didn’t belabor the point.

Ian
2008/08/26

Participate