I have such a diary structure

*** 2019-07-28 Monday 
**** Tasks
*** 2019-07-29 Tuesday
**** Tasks

Desire to capture todos under the Tasks of latest day

(setq org-capture-templates '(("t" "Todo [inbox]" entry (file+headline "~/ORG/src/sdiary.org"
                                                                       "Tasks") "* TODO %i%?")
                              ("T" "Tickler" entry (file+headline "~/ORG/src/tickler.org" "Tickler")
                               "* %i%? \n %U")))

conversely, it collect todos to the first Tasks not the tasks at the bottom of the file.

How could capture todos to the last tasks rather than the first tasks.

  • I don't think that's possible unfortunately. The code for file+headline (but for some of the other plausible choices, like file+regexp, as well), finds the first occurrence: there is no provision to find the last occurrence. You would have to change the code in the function org-capture-set-target-location to do what you want. Have you considered storing your diary in the opposite order (latest time first) instead? – NickD Jul 30 at 3:59
  • Have a look at the available options and see if anything there suits your needs (including, but not limited to the properties section ....): orgmode.org/manual/Template-elements.html – lawlist Jul 30 at 4:22

You can use filename+function as described in the manual:

‘(file+function "filename" function-finding-location)’

A function to find the right location in the file.

The following function moves the point to the last headline matching **** Tasks:

(defun my-org-goto-last-tasks-headline ()
  "Move point to the last headline in file matching \"**** Tasks\"."
  (goto-char (point-max))
  (re-search-backward "^\\*\\*\\*\\* Tasks"))

Then use this function in your capture template as follows:

(setq org-capture-templates
      '(("t" "Todo [inbox]" entry
         (file+function "~/ORG/src/sdiary.org"
                        my-org-goto-last-tasks-headline) "* TODO %i%?")
        ("T" "Tickler" entry
         (file+headline "~/ORG/src/tickler.org" "Tickler") "* %i%? \n %U")))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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