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 Nth β%^{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-promptand:tree-typeproperties. Please rewrite your date/week-tree targets usingfile+olp+datetreesince 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.β©