I have an org file where the majority of the text in the buffer at any one time is folded. I would like to be able to perform a search and replace on only the unfolded lines. For example, I have a file that looks like the below, where Sections A1 through A4 are parent headings to other content, which is currently folded and therefore invisible.

Before operation

* Section A
** Section A1...
** Section A2...
** Section A3...
** Section A4...

I want to be able to select, say, sections A2 and A3 and append some text to just those lines, not any of the child content of those nodes.

After operation (text expanded for illustration only)

* Section A
** Section A1...
** Section A2 <appended text>
*** Foo
** Section A3 <appended text>
*** Foo
** Section A4...

All the search and replaces methods I have tried so far also apply the replacement on the child content (the Foo lines here), which is not the behaviour I desire.

  • You seem to entertain a strange definition of visible: in the before the operation view, all headlines are visible - only their bodies are invisible. Moreover, you cannot make some of the headlines invisible through Org mode means; although, to be sure, you can use lower-level methods to do that, but AFAIK there is no "point-and-click" way to say "make this invisible". In the absence of such a way, your stated purpose I want to be able to select, say, sections A2 and A3 and append some text to just those lines, not any of the child content of those nodes has nothing to do with ...
    – NickD
    Jun 5, 2023 at 16:51
  • ... visibility, at least at the Org mode level. You might want to decouple your question from it: as it stands, it's conflating two issues that don't have anything to do with each other (again at the Org mode level). Alternatively, you might ask for a "point-and-click" way to make things invisible and then apply the answer below. But that's just one way for your problem to be solved and you probably don't want to specify how it should be done.
    – NickD
    Jun 5, 2023 at 16:52
  • I don't really follow that tbh but I have hopefully asked a simpler question here: emacs.stackexchange.com/questions/77505/… Jun 5, 2023 at 19:06

1 Answer 1


As per Make isearch skip folded content in org-mode

(add-hook 'org-mode-hook
      (lambda ()
        (make-local-variable 'search-invisible)
        (setq search-invisible nil)))
  • Does this take affect for all org files? I tried executing the bottom two lines only, with one org file loaded, and regexp-replace doesn't ignore the folded lines. Jun 5, 2023 at 14:46
  • I tested this with an open org file and doing eval (setq search-invisible nil) and the query-replace did not change text in folded sections. I did not test regexp-replace as I overread. Jun 5, 2023 at 15:14
  • OK, yes it does seem to work, but only sometimes. E.g. regexp-replace $ with foo causes foo to be appended to every line (bad). Conversely regexp-replace bar with foo works as expected. Jun 5, 2023 at 15:44

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.