Search code examples
emacsorg-mode

Org-mode: How do I fold all levels but the current in a sparse tree?


After doing C-c / to create a sparse tree, move the cursor to some interesting place and C-c C-c to remove highlights I'd like to be able to collapse all levels but the current one.

Is there some way doing it? I'd like to maintain the cursor positioned in the same place.


Solution

  • You can call org-cycle with a C-u prefix to collapse all trees and then call org-reveal: C-u <TAB> C-c C-r. (The prefix actually cycles the whole buffer between 3 states, but after creating a sparse tree, this seems to always go to overview state first.) Since you'll be adding a C-c C-c at either end to remove the highlights, you could wrap all of this into a single command that you give a shorter keybinding.

    (defun org-sparse-finish ()
      (interactive)
      (org-remove-occur-highlights)
      (org-overview)
      (org-reveal))