After editing at a deeper sublevel I often have to do multiple C-c C-u's (outline-up-heading) to get to the proper level before doing C-c C-e C-s to export the subtree. Is there a way to automate the process of searching upwards for the closest level flagged to be exported?

  • Use org-up-heading-safe in a loop, checking at each point for whatever condition you are looking for.
    – NickD
    Oct 23, 2023 at 16:00

1 Answer 1


You don't specify how the relevant subtree is flagged, but let's assume that it has an export tag. Then the following function will move point up to the closest heading that is explicitly tagged export. The function uses org-up-heading-safe in a loop, as mentioned in my comment: it will stop at the closest heading that is tagged export or if there is no such tag, it will stop at the top level heading that contains the current node:

(defun my/org-up-to-export ()
  (let ((found (member "export" (org-get-tags nil t)))
    (while (and (not found) (not top-level))
      (setq top-level (not (org-up-heading-safe)))
      (setq found (member "export" (org-get-tags nil t))))))

org-up-heading-safe returns the level of the heading that it moved to, or nil if it was not able to move - see its doc string with C-h f org-up-heading.

The function also uses org-get-tags at each level to get the tags list of the current heading - the second argument being t specifies that we are looking for an explicit "local" tag, disregarding inheritance; see the doc string of the function with C-h f org-get-tags.

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.