5

In org mode I go to a subtree. This subtree contains lots of text, let's say a thousand lines. I want to look up some information in it. I got it, but now I want to collapse that subtree I'm in directly with a keystroke. That's all. But it seems that I need to go with the cursor to the heading before I can use TAB for visiblity cycling. Jumping to the heading takes time and is tedious. When I use TAB inside the subtree text nothing happens and I haven't found any other way to collapse the subtree directly.

I looked at the manuals and internet but I couldn't find any solution. Stackexchange is my last resort. Since I'm not a coder I can't help myself here programming a lisp function to solve the problem. Additonally I think this is useful also for other users and I would have needed it very often myself.

4 Answers 4

8

Something like this ?

(defun my-cycle-previous-heading ()
  (interactive)
  (outline-previous-heading)
  (org-cycle))

(define-key org-mode-map (kbd "C-x C-l") 'my-cycle-previous-heading)
3
  • Perfect, thank you so much. The other solutions deserve credit too, but they involve an intermediate step. I need this daily and often, so I go with your custom function.Though I changed the the keybinding to "C-c d" to be in line with the other org fuctions.
    – Jens Lange
    Commented Oct 19, 2016 at 11:51
  • 1
    Excellent! Though now in 2019, emacs 25.3, I had to change the last line into: (global-set-key (kbd "C-x C-l") 'my-cycle-previous-heading)
    – mistige
    Commented May 8, 2019 at 16:06
  • 1
    Depending what you want exactly it may be worth to add a (save-excursion before (outline-previous-heading) (and closing with an ) behind (org-cycle).
    – student
    Commented Jan 23, 2021 at 15:10
5

The Org manual section 2.4 Motion describes the key bindings to be used for navigation.

For your case I think

 C-c C-p   (outline-previous-visible-heading)  
                 Previous heading. 

would cater the requirement. When you are within a sub heading with lots of lines just use C-c C-p to navigate to the heading and press the Tab key to collapse the heading.

1
  • 1
    Your solution is nearly as good as the custom function solution. Thanks for showing the relevant section in the manual. I don't know why I overlooked it, it might be the function name that is not so clear to me.
    – Jens Lange
    Commented Oct 19, 2016 at 12:06
2

C-u <tab> (#'org-cycle) will collapse all top-level trees. This might or might not be what you want.

Alternately, you can move up to the parent heading (repeating it to go more than one level up) with C-c C-u (#'outline-up-heading), and then press tab to collapse it.

2
  • Neither I want to collapse all level nor I want to go up a heading. It works when going up a heading, collapsing it, opening it and then going down to the heading below. It would be still faster than going manually up and collapsing it but the solutions above are more fitting. Thanks for looking at the issue anyway.
    – Jens Lange
    Commented Oct 19, 2016 at 12:12
  • @phils that was meant to be <kbd>C-u &lt;tab&gt;</kbd>, but I forgot that the parser would treat <tab> as an html entity. Fixed now, thanks.
    – zck
    Commented Oct 19, 2016 at 18:52
0

This is an old question, but I recently ran into this. The following will allow you to use TAB for cycling below a heading.

(setq org-cycle-emulate-tab 'white)

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.