1

I am trying to use find-name-dired and query-replace-regexp to find and replace some blocks of text across multiple HTML files. (Just what is explained here in the Emacs Manual.)

query-replace-regexp fails to match anything containing a newline.

For example, I want to find all files containing the following:

      <div class="copyright">

It will find <div class="copyright"> by itself, but not when concatenated with the newline as above.

I've tried coding the newline like so:

  1. <div class="copyright">^J (inserted with C-q C-j)
  2. <div class="copyright">\n
  3. <div class="copyright">"\n".

The first fails with no matches. The second and third cause the minibuffer to complain that \n and "\n" don't match newlines in this function.

I know the newlines in my files are line feeds and not carriage returns (as documented here in Xah Emacs), but I've also tried coding my regular expressions with ^M and \r for good measure. No matches.

How can I get query-replace-regexp to match a newline?

7
  • 1
    Please show your code or more details. It's hard (for me) to guess what you're doing. I suggest you simplify the question by removing the use of find-name-dired from it. It seems to really be a question about using query-replace-regexp. For that, show or explain just what inputs you provide to that command. (And interactively, C-q C-j is indeed the way to insert a newline char.)
    – Drew
    Apr 16, 2021 at 15:59
  • Just what is the data is that you want to match - does it contain 1, 2, or 3 newline chars? Is there any other whitespace involved (e.g. before or after the newline chars)?
    – Drew
    Apr 16, 2021 at 16:00
  • 1
    Are you doing the query-replace-regexp interactively or from a lisp program? In the former case, either use a region that includes all the instances you want to replace or make sure that they are all between point (where your cursor is) and the end of the buffer (see the doc string of query-replace-regexp).
    – NickD
    Apr 16, 2021 at 17:46
  • @Drew I've added a link to the part of the Emacs Manual that details what I'm trying to do. The data I'm trying to match is exactly what's in the large code block of my question: the "copyright" class div followed immediately by a newline. Through testing, I've narrowed down to the newline specifically causing the match failure. I have no problems matching whitespace.
    – zcal
    Apr 17, 2021 at 19:03
  • @NickD I'm using query-replace-regexp interactively as detailed in the link to the Emacs Manual that I added to the question.
    – zcal
    Apr 17, 2021 at 19:06

1 Answer 1

1

First, note that query-replace-regexp matches either from the point to the end of the buffer or in the active region. So if your point is after what you are trying to match it will not work.

With that out of the way, the following works for me:

M-x query-replace-regexp RET <div class="copyright"> C-q C-j RET test RET

And you should get a match. C-q simply inserts whatever character you give it. Just pressing C-j would have been interpreted as RET by Emacs.

1
  • I found this answer because I have the same problem, and this answer doesn't address the original question or mine. You can use a newline as you describe when doing query-replace-regexp in one buffer, but it does not work when trying to edit multiple buffers selected in dired, as the original question specifies with the link. If you have large numbers of files to change, dired is indispensible.This might well be a bug in Emacs. Mar 15 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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