org-capture templates

We've talked about how to configure org-capture and the types of captures we can create. On that second topic, we showed the default templates for each type of note. On this article, we'll focus on templates.

To lay out a definition, a template is the chunk of text that will be inserted by default into our note.

They work as I've shown on the first part of this series, by adding a string as the fourth parameter for a note type:

  (setq org-capture-templates
          ("i" "Post idea"           entry (file "~/Dropbox/wordpress_rake/")
           "* %?\n")           ; <--
          ("w" "Woodworking project" entry (file "~/Dropbox/org-files/")
           "* TODO %?\n")      ; <--

But, as you can see on the example, there's more to it than just a plain string. org-capture templates allow template expansion codes.

Expansion codes

I won't explain each expansion with my own words, because they're very well explained on the org-capture documentation. But I'll copy them here so we can have them at hand.

%[file]Insert the contents of the file given by file.
%(sexp)Evaluate Elisp sexp and replace with the result. The sexp must return a string.
%<…>The result of format-time-string on the … format specification.
%tTimestamp, date only.
%TTimestamp, with date and time.
%u, %ULike the above, but inactive timestamps.
%iInitial content, the region when capture is called while the
region is active.
The entire text will be indented like %i itself.
%aAnnotation, normally the link created with org-store-link.
%ALike %a, but prompt for the description part.
%lLike %a, but only insert the literal link.
%cCurrent kill ring head.
%xContent of the X clipboard.
%kTitle of the currently clocked task.
%KLink to the currently clocked task.
%nUser name (taken from user-full-name).
%fFile visited by current buffer when org-capture was called.
%FFull path of the file or directory visited by current buffer.
%:keywordSpecific information for certain link types, see below.
%^gPrompt for tags, with completion on tags in target file.
%^GPrompt for tags, with completion all tags in all agenda files.
%^tLike %t, but prompt for date. Similarly %^T, %^u, %^U.
You may define a prompt like %Birthdayt.
%^CInteractive selection of which kill or clip to use.
%^LLike %^C, but insert as link.
%proppPrompt the user for a value for property prop.
%promptprompt the user for a string and replace this sequence with it.
%\1 … %\NInsert the text entered at the Nth %prompt, where N is a number, starting from 1.
%?After completing the template, position cursor here.

— Note:


For the prompt keyword you may specify a default value and a completion table with %prompt|default|completion2|completion3…. The arrow keys access a prompt-specific history.