Today I was looking through Xah Lee’s ErgoEmacs Google+ Page (there’s a ton of interesting stuff there—you should check it out) when I came across this post in which someone asks how to find links in an Org mode buffer. Later, in the comments, someone else asks how to locate the headers in an Org mode buffer. Serendipitously, a bit later I came across this post in the Org Mode mailing list that gives a partial answer to the question.
That got me thinking that being able to find these quickly would be very useful. For example, I have all my tax information in an Org Mode file with lots of headings and subheadings for various categories. Being able to show a list of all the headings and then being able to click on one and go right to it is a real time saver, especially when the trees are collapsed. As Marc-Oliver Ihm suggest in his post to the mailing list, the easy way to do this is with occur
.
My solution involves 4 functions. The first is a utility function that does all the work:
(defun find-org-markers (regexp) (occur regexp) (pop-to-buffer "*Occur*"))
It takes a regular expression as input, searches for matching lines with occur
, and then pops to the *Ocurr*
buffer so that you can choose one.
The next two functions search for Org headers
(defun find-top-org-headers () (interactive) (find-org-markers "^\\*[^*]")) (defun find-all-org-headers () (interactive) (find-org-markers "^\\*+"))
The first, find-top-org-headers
, finds all the top level headers (those beginning with a single star). The second, find-all-org-headers
, finds all the headers.
Finally, find-org-links
locates the outbound links in an Org Mode buffer:
(defun find-org-links () (interactive) (find-org-markers "\\[\\["))
I’m not completely satisfied with this function because it just searches for the [[
that begins a link. I would rather use the regexp \\[\\[.*\\]\\]
but occur
won’t match over more than one line and links frequently span two or more lines. Still, it works well enough to be useful.
These are very simple functions but useful nonetheless. Perhaps you will find them useful too.
Update: Jonathan notes that it’s a good idea to add a \\b
to the end of the regexps in find-top-org-headers
and find-all-org-headers
so that they won’t match, for example, bold text that comes at the beginning of a line.
Update 2: Well that seemed like a good idea but the \\b
doesn’t work. Instead, replace the [^*]
in find-top-org-headers
with a space and add a space after the +
in find-all-org-headers
.