I have a bunch of headings scattered around with certain tags.

Is there any way to create a subheading that is somehow a result of all these headings?

I want to eventually export such a subheading to html.

EDIT: Here's an example

I have a project that needs some power tools to be bought, so I will send a list of items to buy to the shop.

In my tools file, I have a few headings with power tools tagged :projecta:. These headings are scattered around the file tool.org

I want to somehow make a subheading where these headings that include the tag :projecta: appears. That way, I can export this subheading to html which I can send to the shop.

EDIT 2: I picture this as a query, which results in some kind of created buffer, so that I can export it.

EDIT 3: Programmatically, this would then be solved like scanning a set of files and extract the headings that contains the tag :projecta: along with their contents and put it into a new buffer.

  • 1
    My best answer here would be: YES, probably this is possible. However, it is not clear what you want and why you want this. Could you add some context and/or provide some example? Also, maybe have a look at stackoverflow.com/help/how-to-ask. Thanks! May 7, 2023 at 13:11
  • You are not specifying whether you want the heading to move to the subheading (BTW, subheading of what?), or whether you want a "virtual" collection of some sort, with the headings remaining in their original places. org-refile is the answer in the first case; I don't know how to do the second case (maybe a sparse tree, but last time I tried I couldn't make it work. See Sparse trees in the manual.)
    – NickD
    May 7, 2023 at 21:04
  • Actually, org-refile can copy the relevant headings to another file, leaving the original headings untouched. It seems to be exactly what you need for what you describe in EDIT 3.
    – NickD
    May 17, 2023 at 2:04

1 Answer 1


Here's an example file tools.org with a couple of code blocks that implement the org-refile strategy in the comments:

* foo                                                                                                         :projecta:

* bar

* baz                                                                                                         :projecta:

* one

* two

* three                                                                                                       :projecta:

* Code                                                                                                        :noexport:

#+begin_src elisp
  (defun my/refile ()
    (let* ((org-refile-keep t)
           (org-reverse-note-order nil)
           (refile-target-file "/tmp/tools-projecta.org")   ; adjust the path to your needs
           (rfloc `("Tools to order" ,refile-target-file nil 1)))
      (org-refile nil nil rfloc)))

  (defun my/refile-by-tag ()
    (org-map-entries #'my/refile "+projecta" 'file))


#+begin_src elisp :results drawer

Before you run the code blocks, create an output file tools-projecta.org with a single headline at the very beginning of the file:

* Tools to order

Now run the first code block with C-c C-c in order to define the function my/refile-by-tag and then run the second code block with C-c C-c in order to run the function. That will refile (by copying - it does not affect the original file) the three :projecta:-tagged entries to tools-projecta.org which you can then export to HTML.

The my/refile-by-tag function works by scanning all the headlines that match the :projecta: tag, using org-map-entries for the scan, and applying the my/refile function on each headline that matches. The my/refile function then calculates a refile location rfloc and calls org-refile with that location. It locally sets org-refile-keep to t for the duration of the org-refile call: that makes org-refile copy, rather than move, the entries. It also makes sure that org-reverse-note-order is nil: org-map-entries scans from top to bottom for the entries that match and that setting makes sure that org-refile refiles them in the same order.

  • ... and another question, another answer and more ... crickets.
    – NickD
    Dec 19, 2023 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.