Narrow buffer, using org-match-sparse-tree?

1.2k Views Asked by At

I would like to filter the buffer in Org-mode to only those headings that match certain tags.

I tried C-c / followed by tag1&tag2. But instead of narrowing the buffer, Emacs just highlights the asterisk next to each heading that has those tags...

How can I change this so that Emacs actually narrows the buffer to those matched headlines?

Thanks!

2

There are 2 best solutions below

3
On

The search regexp used by the original poster is probably incorrect. The sub-function used by org-occur to set the boundaries for the highlighted result is rather simple -- (while (re-search-forward regexp nil t) . . .) The beginning and ending of the matched pattern determine the boundaries of the highlighted area -- i.e., (match-beginning 0), and (match-end 0).

To search for headlines containing either or both tags, the regexp :tag1:\|:tag2: is used when org-occur is called interactively -- i.e., just one backslash and pipe. When org-occur is used NON-interactively, two (2) backslashes and a pipe are required -- e.g., (org-occur ":tag1:\\|:tag2:")

When looking for two tags that are together in a specific order, the regexp looks like this: :tag1:tag2:

The default behavior of org-occur is to fold everything and then unfold only matched results -- with an exception for previously matched results remaining visible, unless the argument keep-previous is non-nil. If the buffer looks completely unfolded (after a search using org-occur), then the regexp that was used probably matched everything.

It is also prudent to check and verify that a current version of org-mode is being used -- M-x org-version RET.


Example of a search using org-occur for the tag :lasc:

Example
(source: lawlist.com)

3
On

Doing C-c / m tag1&tag2 does what you want -- except for level 1 headings (those are always visible, even if they don't contain useful entries; but level 2+ entries are only visible if they match your request).

That works the same way as C-c / t for example (all TODO in the current buffer).