Generate Current Year tag in an Org Capture Template

A crucial aspect of maintaining organized and up-to-date notes is the use of Org Capture templates.

I have currently always hard-coded/set the current year in my org capture templates for use when exporting/constructing my web pages and as a tag for some filtering / web index generation. Of course the main disadvantage of this is that I have to remember to update the year each year, of which I often fail miserably. ☹️

Can I get an org capture template to auto generate just the year as a tag like other capture template attributes?

Well yes, yes you can, and there a couple of ways to achieve this.

Firstly there is the org capture family of format strings which inserts a date when the capture is triggered. This is a bit much for me and instead of generating just a string for the year, each specifier will generate a full timestamp:

However there is another format specifier, which can just insert the year, simply use %<%Y> which is part of a pretty standard common date or time string format options, for example, here are some useful format specifiers that can be used:

Well that was easy, so to insert a year tag I can just wrap colons around %<%Y>

Just for fun, lets try something a little more advanced to achieve the same goal but this time allowing much more flexibility.

A particularly useful functional aspect of an org template is that the dynamic generation of the current year can be achieved using some elisp. Lets use the (format-time-string) function.

Understanding (format-time-string "%Y")

Before diving into the implementation, let’s break down what (format-time-string "%Y") does:

By embedding this function into my Org Capture templates, the inclusion of the current year is neatly automated, this not only reduces manual updates but also ensures consistency across my entries.

Implementing Dynamic Year in Capture Templates

Here’s an example Org Capture template that uses (format-time-string "%Y") to dynamically manage the year for Emacs-related entries:

("e" "Emacs" plain
  (file+function
    "~/DCIM/content/emacs--all.org"
    my-capture-top-level)
  "* TODO %^{title} :emacs:%(format-time-string \"%Y\"):"
:PROPERTIES:
:EXPORT_FILE_NAME: %<%Y%m%d%H%M%S>-emacs--%\\1
:EXPORT_HUGO_SECTION: emacs
:EXPORT_HUGO_LASTMOD: <%<%Y-%m-%d %H:%M>>
:EXPORT_HUGO_CUSTOM_FRONT_MATTER+: :thumbnail /emacs/%<%Y%m%d%H%M%S>-emacs--%\\1.jpg
:END:
%?
" :prepend t :jump-to-captured t)

Key Features of this Template

Benefits of Dynamic Year Management

Comments

comments powered by Disqus