10.1.3 Capture templates
You can use templates for different types of capture items, and for different target locations. The easiest way to create such templates is through the customize interface.
C
β
Customize the variable org-capture-templates
.
Before we give the formal description of template definitions, letβs look at an example. Say you would like to use one template to create general TODO entries, and you want to put these entries under the heading βTasks
β in your file β~/org/gtd.org
β. Also, a date tree in the file βjournal.org
β should capture journal entries. A possible configuration would look like:
(setq org-capture-templates
'(("t" "Todo" entry (file+headline "~/org/gtd.org" "Tasks")
"* TODO %?\n %i\n %a")
("j" "Journal" entry (file+datetree "~/org/journal.org")
"* %?\nEntered on %U\n %i\n %a")))
If you then press t
from the capture menu, Org will prepare the template for you like this:
* TODO
[[file:LINK TO WHERE YOU INITIATED CAPTURE]]
During expansion of the template, β%a
β has been replaced by a link to the location from where you called the capture command. This can be extremely useful for deriving tasks from emails, for example. You fill in the task definition, press C-c C-c
and Org returns you to the same place where you started the capture process.
To define special keys to capture to a particular template without going through the interactive template selection, you can create your key binding like this:
(define-key global-map (kbd "C-c x")
(lambda () (interactive) (org-capture nil "x")))
β’ Template elements | Β Β | What is needed for a complete template entry. |
β’ Template expansion | Β Β | Filling in information about time and context. |
β’ Templates in contexts | Β Β | Only show a template in a specific context. |
10.1.3.1 Template elementsβ
Now lets look at the elements of a template definition. Each entry in org-capture-templates
is a list with the following items:
keysβ
The keys that selects the template, as a string, characters only, for example β"a"
β, for a template to be selected with a single key, or β"bt"
β for selection with two keys. When using several keys, keys using the same prefix key must be sequential in the list and preceded by a 2-element entry explaining the prefix key, for example:
("b" "Templates for marking stuff to buy")
If you do not define a template for the C
key, this key opens the Customize buffer for this complex variable.
descriptionβ
A short string describing the template, shown during selection.
typeβ
The type of entry, a symbol. Valid values are:
entry
β
An Org mode node, with a headline. Will be filed as the child of the target entry or as a top-level entry. The target file should be an Org file.
item
β
A plain list item, placed in the first plain list at the target location. Again the target file should be an Org file.
checkitem
β
A checkbox item. This only differs from the plain list item by the default template.
table-line
β
A new line in the first table at the target location. Where exactly the line will be inserted depends on the properties :prepend
and :table-line-pos
(see below).
plain
β
Text to be inserted as it is.
targetβ
Specification of where the captured item should be placed. In Org files, targets usually define a node. Entries will become children of this node. Other types will be added to the table or list in the body of this node. Most target specifications contain a file name. If that file name is the empty string, it defaults to org-default-notes-file
. A file can also be given as a variable or as a function called with no argument. When an absolute path is not specified for a target, it is taken as relative to org-directory
.
Valid values are:
β(file "path/to/file")
ββ
Text will be placed at the beginning or end of that file.
β(id "id of existing org entry")
ββ
Filing as child of this entry, or in the body of the entry.
β(file+headline "filename" "node headline")
ββ
Fast configuration if the target heading is unique in the file.
β(file+olp "filename" "Level 1 heading" "Level 2" ...)
ββ
For non-unique headings, the full path is safer.
β(file+regexp "filename" "regexp to find location")
ββ
Use a regular expression to position point.
β(file+olp+datetree "filename" [ "Level 1 heading" ...])
ββ
This target1 creates a heading in a date tree2 for todayβs date. If the optional outline path is given, the tree will be built under the node it is pointing to, instead of at top level. Check out the :time-prompt
and :tree-type
properties below for additional options.
β(file+function "filename" function-finding-location)
ββ
A function to find the right location in the file.
β(clock)
ββ
File to the entry that is currently being clocked.
β(function function-finding-location)
ββ
Most general way: write your own function which both visits the file and moves point to the right location.
templateβ
The template for creating the capture item. If you leave this empty, an appropriate default template will be used. Otherwise this is a string with escape codes, which will be replaced depending on time and context of the capture call. You may also get this template string from a file3, or dynamically, from a function using either syntax:
(file "/path/to/template-file")
(function FUNCTION-RETURNING-THE-TEMPLATE)
propertiesβ
The rest of the entry is a property list of additional options. Recognized properties are:
:prepend
β
Normally new captured information will be appended at the target location (last child, last table line, last list item, β¦). Setting this property changes that.
:immediate-finish
β
When set, do not offer to edit the information, just file it away immediately. This makes sense if the template only needs information that can be added automatically.
:jump-to-captured
β
When set, jump to the captured entry when finished.
:empty-lines
β
Set this to the number of lines to insert before and after the new item. Default 0, and the only other common value is 1.
:empty-lines-after
β
Set this to the number of lines that should be inserted after the new item. Overrides :empty-lines
for the number of lines inserted after.
:empty-lines-before
β
Set this to the number of lines that should be inserted before the new item. Overrides :empty-lines
for the number lines inserted before.
:clock-in
β
Start the clock in this item.
:clock-keep
β
Keep the clock running when filing the captured entry.
:clock-resume
β
If starting the capture interrupted a clock, restart that clock when finished with the capture. Note that :clock-keep
has precedence over :clock-resume
. When setting both to non-nil
, the current clock will run and the previous one will not be resumed.
:time-prompt
β
Prompt for a date/time to be used for date/week trees and when filling the template. Without this property, capture uses the current date and time. Even if this property has not been set, you can force the same behavior by calling org-capture
with a C-1
prefix argument.
:tree-type
β
Use week
to make a week tree instead of the month-day tree, i.e., place the headings for each day under a heading with the current ISO week. Use month
to group entries by month only. Default is to group entries by day.
:unnarrowed
β
Do not narrow the target buffer, simply show the full buffer. Default is to narrow it so that you only see the new material.
:table-line-pos
β
Specification of the location in the table where the new line should be inserted. It should be a string like βII-3
β meaning that the new line should become the third line before the second horizontal separator line.
:kill-buffer
β
If the target file was not yet visited when capture was invoked, kill the buffer again after capture is completed.
:no-save
β
Do not save the target file after finishing the capture.
10.1.3.2 Template expansionβ
In the template itself, special β%-escapes"1 allow dynamic insertion of content. The templates are expanded in the order given here:
β%[FILE]
ββ
Insert the contents of the file given by FILE
.
β%(EXP)
ββ
Evaluate Elisp expression EXP
and replace it with the result. The EXP
form must return a string. Only placeholders pre-existing within the template, or introduced with β%[file]
β, are expanded this way. Since this happens after expanding non-interactive β%-escapes", those can be used to fill the expression.
β%<FORMAT>
ββ
The result of format-time-string on the FORMAT
specification.
β%t
ββ
Timestamp, date only.
β%T
ββ
Timestamp, with date and time.
β%u
β, β%U
ββ
Like β%t
β, β%T
β above, but inactive timestamps.
β%i
ββ
Initial content, the region when capture is called while the region is active. If there is text before β%i
β on the same line, such as indentation, and β%i
β is not inside a β%(exp)
β form, that prefix is added before every line in the inserted text.
β%a
ββ
Annotation, normally the link created with org-store-link
.
β%A
ββ
Like β%a
β, but prompt for the description part.
β%l
ββ
Like β%a
β, but only insert the literal link.
β%c
ββ
Current kill ring head.
β%x
ββ
Content of the X clipboard.
β%k
ββ
Title of the currently clocked task.
β%K
ββ
Link to the currently clocked task.
β%n
ββ
User name (taken from user-full-name
).
β%f
ββ
File visited by current buffer when org-capture was called.
β%F
ββ
Full path of the file or directory visited by current buffer.
β%:keyword
ββ
Specific information for certain link types, see below.
β%^g
ββ
Prompt for tags, with completion on tags in target file.
β%^G
ββ
Prompt for tags, with completion all tags in all agenda files.
β%^t
ββ
Like β%t
β, but prompt for date. Similarly β%^T
β, β%^u
β, β%^U
β. You may define a prompt like β%^{Birthday}t
β.
β%^C
ββ
Interactive selection of which kill or clip to use.
β%^L
ββ
Like β%^C
β, but insert as link.
β%^{PROP}p
ββ
Prompt the user for a value for property PROP
.
β%^{PROMPT}
ββ
Prompt the user for a string and replace this sequence with it. You may specify a default value and a completion table with β%^{prompt|default|completion2|completion3...}
β. The arrow keys access a prompt-specific history.
β%\N
ββ
Insert the text entered at the N
th β%^{PROMPT}
β, where N
is a number, starting from 1.
β%?
β
After completing the template, position point here.
For specific link types, the following keywords are defined2:
Link type | Available keywords |
---|---|
bbdb | β%:name β, β%:company β |
irc | β%:server β, β%:port β, β%:nick β |
mh, rmail | β%:type β, β%:subject β, β%:message-id β |
β%:from β, β%:fromname β, β%:fromaddress β | |
β%:to β, β%:toname β, β%:toaddress β | |
β%:date β (message date header field) | |
β%:date-timestamp β (date as active timestamp) | |
β%:date-timestamp-inactive β (date as inactive timestamp) | |
β%:fromto β (either βto NAME" or βfrom NAME")3 | |
gnus | β%:group β, for messages also all email fields |
w3, w3m | β%:url β |
info | β%:file β, β%:node β |
calendar | β%:date β |
org-protocol | β%:link β, β%:description β, β%:annotation β |
10.1.3.3 Templates in contextsβ
To control whether a capture template should be accessible from a specific context, you can customize org-capture-templates-contexts
. Letβs say, for example, that you have a capture template βp" for storing Gnus emails containing patches. Then you would configure this option like this:
(setq org-capture-templates-contexts
'(("p" (in-mode . "message-mode"))))
You can also tell that the command key p
should refer to another template. In that case, add this command key like this:
(setq org-capture-templates-contexts
'(("p" "q" (in-mode . "message-mode"))))
See the docstring of the variable for more information.
- Org used to offer four different targets for date/week tree capture. Now, Org automatically translates these to use
file+olp+datetree
, applying the:time-prompt
and:tree-type
properties. Please rewrite your date/week-tree targets usingfile+olp+datetree
since the older targets are now deprecated.β© - A date tree is an outline structure with years on the highest level, months or ISO weeks as sublevels and then dates on the lowest level. Tags are allowed in the tree structure.β©
- When the file name is not absolute, Org assumes it is relative to
org-directory
.β©