5

Windows 10, Emacs 25.1, Helm

I open 3 different files: enter image description here

I need:

  1. Edit simultaneously all buffers. Add text "customer/" to the node "bodyFileName"in all buffers.

  2. After edit save all buffers,

Here result: enter image description here

Is it possible?

1
  • 1
    I like using the grep-mode and then wgrep to edit multiple buffers and save, and I also use multiple-cursors when in wgrep mode. There are undoubtedly other methods ...
    – lawlist
    Aug 13, 2018 at 17:53

1 Answer 1

4

Maybe you can try to to use multi-occur-in-this-mode. To do that process as follows:

  1. open all the files you want to process (C-x f then *.json)
  2. M-x multi-occur-in-this-mode then enter your search string ("bodyFileName" for instance)
  3. then in the occur buffer, enter edit mode by typing e
  4. here you can do your search and replace as usual C-x % for instance. For each json buffer where a match occurs you will be prompted for modification.
  5. once that you have modified all you want you can leave occur edit mode by C-c C-c
  6. to close occur buffer type q
  7. save all modified buffers: C-x s

Update Omar is right, I have multi-occur-in-this-mode function in my Emacs config for so long.... that I completely forgot that it is not a built-in function but a function described in Mastering Emacs.

(eval-when-compile
  (require 'cl))

(defun get-buffers-matching-mode (mode)
  "Returns a list of buffers where their major-mode is equal to MODE"
  (let ((buffer-mode-matches '()))
   (dolist (buf (buffer-list))
     (with-current-buffer buf
       (if (eq mode major-mode)
           (add-to-list 'buffer-mode-matches buf))))
   buffer-mode-matches))

(defun multi-occur-in-this-mode ()
  "Show all lines matching REGEXP in buffers with this major mode."
  (interactive)
  (multi-occur
   (get-buffers-matching-mode major-mode)
   (car (occur-read-primary-args))))
9
  • I have not command "multi-occur-in-this-mode". I has only "multi-occur-in-matching-buffer". Aug 14, 2018 at 7:25
  • You can also use this command: simply fill "..filenames match regexp" with "json$", then you will be asked for "lines matching regexp" -> enter "bodyFileName", then continue (continue from step 3) Aug 14, 2018 at 8:35
  • 1
    C-x s will ask you for each buffer whethere you want to save it or not, but you can also do C-u C-x s which saves all without prompting.
    – Omar
    Aug 14, 2018 at 18:59
  • 1
    I think the mysterious multi-occur-in-this-mode command, might be from this Mastering Emacs blog post.
    – Omar
    Aug 14, 2018 at 19:03
  • 1
    @Omar "C-x s" also help. After it ask me to save buffer I select "!" and Emacs save ALL edited buffers. Aug 15, 2018 at 7:16

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.