Q: how do I hide all org-mode blocks under the current header?

I want to fold all source code blocks under the current heading only. I know that I can use org-hide-block-all to fold all blocks in the buffer, and org-hide-block-toggle to toggle the current block (i.e., the one at point). But how about all blocks under the current heading?

For example:

* header 1
   #+name: block-1
   #+begin_src R
     x <- 1:10
   #+name: block-2
   #+begin_src R
     y <- 1:10
   #+name: block-3
   #+begin_src R
     z <- x + y
* header 2
   #+name: dont-fold-me-1
   #+begin_src R
     a <- "puppies"
   #+name: dont-fold-me-2
   #+begin_src R
     b <- "kitties"

I'd like to be able to fold all blocks under header 1 but leave the rests of the blocks (under header 2) alone. How would I do that?


How about using buffer narrowing?

(defun my/org-hide-block-subtree ()
  "Hide blocks only below current heading."

It's situations like this that I wish org-map-entries could map over more than just headings...

  • 1
    Thanks! I made a minor edit to use the save-restriction idiom. Feel free to roll it back if you prefer. – Dan Feb 7 '17 at 15:03
  • Great suggestion! save-restriction was new to me :-) – ebpa Feb 7 '17 at 15:58

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.