Looping Through Org Headings

I’ve got several functions that do some sort of processing on Org files. Many of them were written years ago when I was just starting with Org-mode and as a result, they often take a naive approach to, say, locating Org top level headers. My typical solution was to do a regular expression search on something like ^\\*[ *].

There are, of course, better ways and Kaushal Modi over at A Scripter’s Notes shows us one of them. If you want to loop through all Org headers and do something to each entry, Org has a function just for that, org-map-entries. This mapping function will loop through each Org entry and call a function (with no arguments) that can assume the point is at the beginning of the heading.

You can filter the entries to look at by things such as tags, properties, or the level of the header. You can filter on the absence or presence of a tag or tags, or for any tags or properties satisfying a regular expression.

Modi ends his post with an example of adding a property to each heading. The org-map-entries function is versatile and useful and makes dealing with Org files programmatically a bit easier. If you do any Elisp programming involving Org files, be sure to take a look at Modi’s post.

This entry was posted in General and tagged , . Bookmark the permalink.