Org Mode: Reusing the date from file-datetree-prompt

Posted: - Modified: | emacs, org
Update 2015-02-17: Or you can just use %t in your org-capture-templates, as Seth Mason points out in the comments… =)

How can you get Org Mode to create and schedule entries within a year-month-day outline structure? You can define an org-capture-templates with the keyword file+datetree+prompt. This lets you specify a date for your entry, and Org will create the entry in a hierarchy organized by year, month, and day.

If you’d like to display the entry in your agenda, you’ll also need an active timestamp of the form <yyyy-mm-dd>. Fortunately, you can reuse the date you specified at the initial prompt to create the datetree entry. Looking at org-capture.el will show you that the org-capture function refers to the org-read-date-final-answer, which is set to whatever string you entered at the date prompt. For example, if you entered 18, then org-read-date-final-answer will be set to 18. You can use org-read-date to convert this back to a yyyy-mm-dd-style date.

How do you use this in org-capture-templates? You can use the %(...) syntax for calling an Emacs Lisp expression, like so:

(setq org-capture-templates '(
  ;; other entries go here
  ("s" "Journal entry with date, scheduled" entry
   (file+datetree+prompt "~/personal/journal.org")
    "* %^{Title}\n<%(org-read-date nil nil org-read-date-final-answer)>\n%i\n%?\n")))

Here’s sample output from that capture template:

* 2015
** 2015-12 December
*** 2015-12-31 Thursday
**** End of the year party!
<2015-12-31>

Thanks to Sean Miller for the nudge to think about this!

You can view 6 comments or e-mail me at sacha@sachachua.com.

6 comments

Sudhir Khanger

2015-02-17T05:12:35Z

Is Emacs Tutorial (C-h t) enough to begin with Org Mode Manual [1]? I suppose I can pickup more of Emacs as I use more of org-mode.

[1] http://orgmode.org/org.html

Yes, I think the Emacs tutorial will give you a good overview of the basics. If you find the Org Mode manual overwhelming, you might like the compact guide (http://orgmode.org/guide/). You can also browse through the tutorial articles at http://orgmode.org/worg/org... to see if someone's described a workflow that's similar to what you want. =)

Doesn't %t do this? At least it does for me in 8.2.10.

I get the day included in it though (e.g. "<2015-02-09 Mon>"). Is that what you are trying to avoid?

Have I mentioned how much I love how writing about a seemingly-clever way to do something in Emacs will result in someone commenting with a much better, simpler way to do things? =D

For some reason, I had assumed that %t was always today's date, but it isn't. Sweet! Thanks!

Yeah, I did too actually. I had to look at my journal file to double check and saw the date matched the tree date.

Hrmm. now I want the actual timestamp of when I entered in the entry to show up. I guess format-date-string inside of the template is the way to go? Unless you have a better way? :)

[%<%Y-%m-%d %H:%M>] uses the current time, I think. =)