Of Narrowing Subtrees and Focus

On my TODO list yesterday, I had “C-z > narrow subtree.” Translation: Make C-z, which minimizes Emacs (which I only press by mistake and curse when I do), to narrow to the subtree I’m on instead. What is narrow to subtree, and why should you care?

The way I work with projects in org-mode is pretty straightforward. I start from a task, and as this task grows and expands, it becomes a parent with the “ACTIVE” keyword attached and additional sub-tasks with “TODO” keywords below.

This can lead to a messy outcome when I go to my org file with all of its headers and comments from other projects. To focus on one project, I narrow it down to that project’s parent header and its siblings. This is traditionally done with org-narrow-to-subtree.

org-narrow-to-subtree1 has an Emacs key binding I tired and failed to remember several times: C-x n s. It makes sense logically, perhaps, with n for narrow and s for subtree, but the shortcut itself is not “org-mode friendly” enough (most of them are C-c something), so I wanted something better.

Then I realized something else: org-narrow-to-subtree only works one way. You can narrow it down to focus on a subtree, but you can’t go back to the full buffer. I assume there’s a “widen” function, which I can’t find. Instead, there’s org-toggle-narrow-to-subtree, which works both ways: one keypress narrows to a subtree, and another widens back to the buffer. You wouldn’t know of org-toggle-narrow-to-subtree right away because this function is not mapped to any keys by default and is not mentioned in the manual for some reason.

As I was looking more into this, I found out there’s also C-c C-j, org-goto, which is potentially better. Unlike narrow and toggle narrow above, with this command you don’t need to go back up to the parent header. Once you use the key combo (which seems more intuitive than the narrow to subtree variant) you see the tree you’re on in the context of its parents.

This function comes with optional configurations which I’m still reading into.

There’s always more to explore with Emacs!

Footnotes


  1. There’s also its brother, C-x n e, org-narrow-to-element which is similar, but will narrow to other elements on the buffer, like a property drawer for example. In my case, this is not very useful, but I might find a good use case for it as well. ↩︎