5

I can un/fold a section in org-mode by calling org-cycle. However, the cursor has to stay on the heading in order for that to work. What can I do to collapse the section in which I am currently stationed?

Example:

* Section 1
** Section 1.1
** Section 1.2
   lots of lines
   information [] <-- cursor somewhere here
   lots of lines
** Section 1.3

----> fold outer

* Section 1
** Section 1.1
** Section 1.2 [] <--- cursor somewhere here
** Section 1.3

----> fold outer

* Section 1 [] <--- cursor somewhere here

I feel like there should be a way to do this by jumping to the first heading of the outer level and calling org-cycle, but making this jump seems to require some dev knowledge of org-mode, so I am here to ask for help.

1
  • There is and you don't need any dev knowledge for it: you just need to know how to navigate the tree. @cole's answer below tells you how to do that, but you can get an idea of what can be done by clicking on the Org menu item and using the entries under Navigate Headings - they also helpfully tell you what keybinding you can use, so that you can use the menu when you forget, but eventually when your fingers remember the keybindings, you won't need the menu any more. And of course, read the manual: C-h i g (org)Motion.
    – NickD
    Aug 11 at 15:38
3

You can try this function. (surely not perfect)

(defun org-fold-outer ()
  (interactive)
  (org-beginning-of-line)
  (if (string-match "^*+" (thing-at-point 'line t))
      (outline-up-heading 1))
  (outline-hide-subtree)
  )
1
  • 1
    I don't have enough reputation to comment, so I'll add this as another answer: the function proposed by djangoliv needs one small change: beginning-of-line should be replaced with org-beginning-of-line - otherwise consecutive folding (i.e. "resurfacing" from nested headings) may get stuck sometimes. I'm not sure about the exact cause of this behavior - probably an empty line before one of the headings - but anyway, org-beginning-of-line fixed this problem for me (org-mode 9.1.9).
    – xor-xor
    Mar 14 '20 at 20:23
2

There are some predefined settings available for the behavior of org-cycle.

You can play with the settings using

M-x customize-variable org-cycle-emulate-tab
1
  • 1
    good to know about this option: with (setq org-cycle-emulate-tab nil), org-cycle on a non-heading achieves the desired result (part 1 of the example). Staying on top of a heading, however, simply unfolds that heading instead of the folding the upper level (part 2 of the example). I am still to see what's more useful for me, so thanks! May 29 '18 at 11:14
2

Here is a simple way to do it without custom keybinds.

1) A simple solution for part 1 is to use org-previous-visible-heading which is bound to C-c C-p. This will take you to to the heading you are working under, at which point you can hit TAB to collapse it.

Taken together C-c C-p TAB will navigate to the heading you are working under and collapse it.

Note: There is also the handy C-c C-n for going to the next visible org header (we are just prefixing the next and previous line commands with C-c to get this behaviour)

2) Use outline-up-heading (C-c C-u) to navigate to the parent heading, then hit tab to collapse it's children.

C-c C-u TAB

This is very similar to C-c C-p except it is guaranteed to go to the parent heading whereas C-c C-p will go to the previous visible heading. So in example 2 C-c C-p will go to Section 1.1 while C-c C-u will go to Section 1.

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.