8

Is there a way to fold a region in org-mode? (Not the whole file, and not an individual item, but a region containing multiple items.)

Often I want to collapse only a portion of an org file, say folding:

* a
** a1
** a2
* b
** b1
** b2
** b3
* c
** c1
** c2
* d
** d1
** d2

into

* a
** a1
** a2
* b ...
* c ...
* d
** d1
** d2

I know in this particular example I could simply fold the whole file using org-cycle, then expand a and d individually, but imagine there are hundreds of items and I want to fold a dozen contiguous items contained in an active region while leaving the rest of the items in the file expanded.

  • How would this be different than using the C-tab functionality? Using shift-tab cycles the entire file, but C-tab folds a particular tree. – GJStein Jul 12 '15 at 19:43
  • Have you tried recording a keyboard macro? – Melioratus Jul 12 '15 at 20:53
  • @Melioratus I would rather use a built-in feature or elisp since I do this very often. – holocronweaver Jul 12 '15 at 21:30
  • @GJStein I have updated the question which hopefully clarifies what I am seeking. – holocronweaver Jul 12 '15 at 21:30
7

This seems to do what you want :

(defun org-hide-subtrees-in-region (beg end)
  (interactive "r")
  (save-restriction
    (narrow-to-region beg end)
    (outline-hide-sublevels 1)))

EDIT: I first gave a wrong solution, which I keep here as it is also interesting IMO (but doesn't answer the question)

The function outline-hide-region-body seems to be exactly what you want, but for some reason it is not interactive. We could add an interactive spec to it, but let's just define org-hide-subtrees-in-region as :

(defun org-hide-subtrees-in-region (beg end)
  (interactive "r")
  (outline-hide-region-body beg end))
  • Excellent, this works! Would it be possible to make this behave like org-cycle on the region? – holocronweaver Jul 12 '15 at 23:48
  • @holocronweaver I don't have an obvious solution in mind. – YoungFrog Jul 13 '15 at 9:49

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.