If we have org file with datetree and repeating actions, how can I capture under last heading of some level. Let's say we have this:

* 2018
** 2018-W47
*** 2018-11-21 Wednesday    
**** Gym Session
** 2018-W48
*** 2018-12-01 Saturday
**** Running
*** 2018-12-02 Sunday
**** Gym Session

I want to create org-capture template which would put entry under last "Gym Session" from Sunday. This means I can't use heading, because it's not guaranteed to be unique. Is there some function like "org-last-heading-of-level-4" or something similar maybe? Thanks!

2 Answers 2


From your description the selection of the heading you want to add to is quite random. At least, the description of your work flow is not detailed enough for someone else to create a function that always finds the correct header.

I suggest that you use org-mode's clocking functionality to log into the task of under the right heading and use the current task capture template from the howardism blog to add more text:

(add-to-list 'org-capture-templates
             `("c" "Item to Current Clocked Task" item
               "%i%?" :empty-lines 1))
  • Ok, this does seem interesting feature and I will dig further because it actually might be better approach to my problem than my own, but I wanted something that seamed way simpler than this. It's not really random, because I want something to go to the last existing heading of certain level.
    – Djole
    Dec 9, 2018 at 11:59

Again, this might not again be exactly what you want, but gives the kind of functionality you need. In your example, you want to add text to the end of the file.

I am using this function myself to add comments to a last captured org note in an org datetree file. The function simply goes to the end of the file so that I can write whatever I want:

(defun my/last-captured-org-note ()
  "Move to the last line of the last org capture note."
  (find-file "~/Dropbox/org/notes.org")
  (goto-char (point-max)))

If the header you want is not the last one, you can add approriate search-backward and forward-line forms to the function to get there.

  • Great, this can work with search-backward. Thanks!
    – Djole
    Dec 11, 2018 at 9:54
  • It may be because I'm new on this site, but I can't mark your answer as accepted. If there is some option to do it that I don't see, please tell me. Thanks.
    – Djole
    Dec 11, 2018 at 10:02
  • You might have to upvote the answer first before you can mark it accepted.
    – Heikki
    Dec 12, 2018 at 6:07

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.