1

I am searching and replacing text within all marked files in a Dired buffer, as per this post:

https://emacs.stackexchange.com/a/9497/37866

I am successful until the point where it finds the first instance of my query. Typing either SPC or y results in Emacs telling me the buffer is in read-only mode. How can I replace that match anyway, and then move on to the next match? string is ready to be replacedstring can't be replaced because buffer is read only

2 Answers 2

1

When you hit SPC or y you're telling Emacs to change the text at the search hit. If its buffer is in read-only mode then Emacs tells you it can't change the text because it's read-only.

Unfortunately, the doc for Q (command dired-do-find-regexp-and-replace) doesn't tell you anything at all about the dialog that it initiates, what keys you can press, and what they do. The author likely didn't bother, just assuming that you would (somehow?!!?) know that the command is based on the behavior of query-replace. Or else that you would read the prompt that tells you you can hit ? to find out what you can do.

Hitting ? during the query-replacing shows you this:

  • Type Space or y to replace one match, Delete or n to skip to next,
  • RET or q to exit,
  • Period to replace one match and exit,
  • Comma to replace but not move point immediately,
  • C-r to enter recursive edit (C-M-c to get out again),
  • C-w to delete match and recursive edit,
  • C-l to clear the screen, redisplay, and offer same replacement again,
  • ! to replace all remaining matches in this buffer with no more questions,
  • ^ to move point back to previous match,
  • u to undo previous replacement,
  • U to undo all replacements,
  • E to edit the replacement string.

In multi-buffer replacements type Y to replace all remaining matches in all remaining buffers with no more questions, N to skip to the next buffer without replacing remaining matches in the current buffer.

So one thing you can do, when you find out that the buffer where you want to make a change, is to use "C-r to enter recursive edit", then use C-x C-q to toggle read-only-mode, then use "C-M-c to get out again" (that is, end the recursive edit), then hit y or SPC to make one or more changes in that buffer.


FWIW, I just filed Emacs bug #55050, asking that the info you get by hitting ? during query-replacement be added (directly or indirectly) to the doc for each command that makes use of that dialog.

0

It turns out that all I needed to do was change all the files' permissions to read-write using the linux command "chmod 666 i.*". Then you can type "y" or space to make the change. You can hold down to go through all of them quickly. Then C-x C-c to close out of the buffer and it will ask you if you want to save every file that was changed, then you once again hold down "y" or space.

All my steps go as follows:

  1. make sure all files you might need to edit are writeable "chmod 666 *". To make sure, type "ls -l" and make sure they have -rw-rw-rw- listed by relevant files.
  2. navigate inside the directory with all the files you want want to search through and possible change a string inside them.
  3. open emacs buffer "emacs -nw"
  4. enter command mode M-x (hold down meta key and x at same time, varies with type of keyboard)
  5. type "dired" and hit enter
  6. hit enter (asks which directory to work in, but you already are in the correct directory)
  7. type "%m" that is, shift-5 then "m"
  8. mark all files to search: in my case I was searching through all files that started with "i." so I said "i.*"
  9. Enter query-replace mode: type "Q" that is shift-q
  10. type in search string (spaces matter) and hit enter
  11. type in replace string and hit enter
  12. press "y" to replace each occurrence (or hold down y)
  13. once you have replaced the last one, emacs goes back into regular old edit mode of the last file it found. So now C-x C-q to quit the buffer.
  14. now emacs will ask you if you want to save each file (individually). Hold down "y" to save all of your changes.

As you can tell, I don't know the "proper" vocab for navigating emacs. But I tried to be as detailed as possible for my future self who will inevitably forget this whole process.

Your Answer

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

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