Providing values to functions in org-capture-templates

Posted: - Modified: | emacs, org

Over at the Emacs StackExchange, Raam Dev asked how to define functions for org-capture-templates that could take arguments. For example, it would be useful to have a function that creates a Ledger entry for the specified account. Functions used in org-capture-templates can’t take any arguments, but you can use property lists instead. Here’s the answer I posted.

You can specify your own properties in the property list for the template, and then you can access those properties with plist-get and org-capture-plist. Here’s a brief example:

Here’s a brief example:

(defun my/expense-template ()
  (format "Hello world %s" (plist-get org-capture-plist :account)))
(setq org-capture-templates '(("x" "Test entry 1" plain
                               (file "~/tmp/test.txt")
                               (function my/expense-template)
                               :account "Account:Bank")
                              ("y" "Test entry 2" plain
                               (file "~/tmp/test.txt")
                               (function my/expense-template)
                               :account "Account:AnotherBank")))

I hope that helps!

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

2 comments

Emmanuel Goldstein

2019-01-30T09:42:34Z

What if I want to call a function once I have called org-capture-templates and selected a template? For instance, I have a function that has a series of predefined entries already, and it'd be handy to call that once I capture. Any ideas?

Greg Coladonato

2021-02-04T18:24:46Z

It helped! Maraming Salamat Sacha! See you on the Emacs SF call again some time :)