Skip to main content
2 of 3
explained why replacement fails in ivy-occur buffer

How to do multi-line regex replace across multiple files?

Is there a convenient way (with preview, interactivity) to replace regex multi-line matches across many files?

I know how to do this for single-line matches. I use counsel-rg, open ivy-occur buffer with C-c o, and can make my edits there.

Multi-line search works, and I can get the replacement candidates, BUT in ivy-occur buffer file names + line numbers interfere heavily with replacement.

The crux of the problem is that file-names and line numbers are read-only, and regex-replace fails if any part of this text is matched.

Here's an example of search results ivy-occur for regex :PROPERTIES:\n.*?\n:END::

60 candidates: ./README.org:1::PROPERTIES: ./README.org:2::header-args: :comments link :noweb yes ./README.org:3::END: ./README.org:6::PROPERTIES: ./README.org:7::ID: 41e08286-2ccc-4c5b-b1ed-10d7dde499d3 ./README.org:8::END: ... 

I can get creative, and use something like negative look-behind (?<=org:\d*?:) from pcre2el package, to make edits line-by-line and not touching read-only text in the buffer. But this seems like a lot of hoops to jump through for a simple operation.