GNU Emacs
Org
Capture and Attachments

Capture and Attachments

An important part of any organization system is the ability to quickly capture new ideas and tasks, and to associate reference material with them. Org does this using a process called capture. It also can store files related to a task (attachments) in a special directory. Finally, it can parse RSS feeds for information. To learn how to let external programs (for example a web browser) trigger Org to capture material, see Protocols for External Access.

Capture

Capture lets you quickly store notes with little interruption of your work flow. Org's method for capturing new items is heavily inspired by John Wiegley's excellent Remember package.

Setting up capture

The following customization sets a default target file for notes.

(setq org-default-notes-file (concat org-directory "/notes.org"))

You may also define a global key for capturing new material (see Activation).

Using capture

M-x org-capture (org-capture)

Display the capture templates menu. If you have templates defined (see Capture templates), it offers these templates for selection. It inserts the template into the target file and switches to an indirect buffer narrowed to this new node. You may then insert the information you want.

C-c C-c (org-capture-finalize)

Once you have finished entering information into the capture buffer, C-c C-c returns you to the window configuration before the capture process, so that you can resume your work without further distraction. When called with a prefix argument, finalize and then jump to the captured item.

C-c C-w (org-capture-refile)

Finalize the capture process by refiling the note to a different place (see Refile and Copy). Please realize that this is a normal refiling command that will be executed—so point position at the moment you run this command is important. If you have inserted a tree with a parent and children, first move point back to the parent. Any prefix argument given to this command is passed on to the org-refile command.

C-c C-k (org-capture-kill)

Abort the capture process and return to the previous state.

You can also call org-capture in a special way from the agenda, using the k c key combination. With this access, any timestamps inserted by the selected capture template defaults to the date at point in the agenda, rather than to the current date.

To find the locations of the last stored capture, use org-capture with prefix commands:

C-u M-x org-capture
Visit the target location of a capture template. You get to select the template in the usual way.
C-u C-u M-x org-capture
Visit the last stored capture item in its buffer.

You can also jump to the bookmark org-capture-last-stored, which is automatically created unless you customize org-bookmark-names-plist.

To insert the capture at point in an Org buffer, call org-capture with a C-0 prefix argument.

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+olp+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

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 select 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-spec>. If it 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 <file-spec>)
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 <file-spec> "node headline")
(file+headline <file-spec> function-returning-string)
(file+headline <file-spec> symbol-containing-string)
Fast configuration if the target heading is unique in the file.
(file+olp <file-spec> "Level 1 heading" "Level 2" ...)
(file+olp <file-spec> function-returning-list-of-strings)
(file+olp <file-spec> symbol-containing-list-of-strings)
For non-unique headings, the full path is safer.
(file+regexp <file-spec> "regexp to find location")
Use a regular expression to position point.
(file+olp+datetree <file-spec> [ "Level 1 heading" ...])
(file+olp+datetree <file-spec> function-returning-list-of-strings)
(file+olp+datetree <file-spec> symbol-containing-list-of-strings)

This target30 creates a heading in a date tree31 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 <file-spec> function-finding-location)
A function to find the right location in the file.
(clock)
File to the entry that is currently being clocked.
(here)
The position of point.
(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 file(note: When the file name is not absolute, Org assumes it is relative to org-directory.), 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
Default is to group entries by day. 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. Use any subset of (year quarter month week day) to group by the specified levels. In case month and week are both specified, weeks are assigned to the month containing Thursday, to be consistent with the ISO year-week rule. In case quarter and week but not month are specified, quarters are 13-week periods; otherwise they are 3-month periods.

:tree-type can also be a function, in which it should take the date as an argument and generate a list of pairs for org-datetree-find-create-hierarchy.

: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.
:refile-targets
Temporarily set org-refile-targets to the value of this property.
:hook
A nullary function or list of nullary functions run before org-capture-mode-hook when the template is selected.
:prepare-finalize
A nullary function or list of nullary functions run before org-capture-prepare-finalize-hook when the template is selected.
:before-finalize
A nullary function or list of nullary functions run before org-capture-before-finalize-hook when the template is selected.
:after-finalize
A nullary function or list of nullary functions run before org-capture-after-finalize-hook when the template is selected.
Template expansion

In the template itself, special "%-escapes"(note: If you need one of these sequences literally, escape the % with a backslash.) 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. Examples: %(org-id-new), %(eval default-directory)
%<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.
%L
Like %l, but without brackets (the link content itself).
%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
Username (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. You may specify a default value with %^{PROP|default}.
%^{PROMPT}X, X is one of g,G,t,T,u,U,C,L
Prompt the user as in %^X, but use the custom prompt string. You may specify a default value and completions with %^{PROMPT|default|completion1|completion2|completion3...}X.
%^{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} (but not %^{PROMPT}X), where /N is a number, starting from 1.
%\*N
Same as %\N, but include all the prompts.
%?
After completing the template, position point here.

For specific link types, the following keywords are defined(note: If you define your own link types (see Adding Hyperlink Types), any property you store with org-link-store-props can be accessed in capture templates in a similar way.):

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")32
gnus %:group, for messages also all email fields
w3, w3m %:url
info %:file, %:node
calendar %:date
org-protocol %:link, %:description, %:annotation
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.

Attachments

It is often useful to associate reference material with a heading or subtree. Small chunks of plain text can simply be stored in the subtree of a project. Hyperlinks (see Hyperlinks) can establish associations with files that live elsewhere on a local, or even remote, computer, like emails or source code files belonging to a project.

Another method is attachments, which are files located in a directory belonging to heading/subtree. Org uses directories named either by the ID property of a heading or by a DIR property.

Attachment defaults and dispatcher

By default, Org attach uses ID properties when adding attachments to headings. This makes working with attachments fully automated. There is no decision needed for folder-name or location. ID-based directories are by default located in the data/ directory, which lives in the same directory where your Org file lives(note: If you move entries or Org files from one directory to another, you may want to configure org-attach-id-dir to contain an absolute path.).

When attachments are made using org-attach a default tag ATTACH is added to the node that gets the attachments.

For more control over the setup, see Attachment options.

The following commands deal with attachments:

C-c C-a (org-attach)

The dispatcher for commands related to the attachment system. After these keys, a list of commands is displayed, and you must press an additional key to select a command:

a (org-attach-attach)

Select a file and move it into the task's attachment directory. The file is copied, moved, or linked, depending on org-attach-method. Note that hard links are not supported on all systems.

c=/=m=/=l

Attach a file using the copy/move/link method. Note that hard links are not supported on all systems.

b (org-attach-buffer)

Select a buffer and save it as a file in the task's attachment directory.

n (org-attach-new)

Create a new attachment as an Emacs buffer.

z (org-attach-sync)

Synchronize the current task with its attachment directory, in case you added attachments yourself.

o (org-attach-open)

Open current task's attachment. If there is more than one, prompt for a file name first. Opening follows the rules set by org-file-apps. For more details, see the information on following hyperlinks (see Handling Links).

O (org-attach-open-in-emacs)

Also open the attachment, but force opening the file in Emacs.

f (org-attach-reveal)

Open the current task's attachment directory.

F (org-attach-reveal-in-emacs)

Also open the directory, but force using Dired in Emacs.

d (org-attach-delete-one)

Select and delete a single attachment.

D (org-attach-delete-all)

Delete all of a task's attachments. A safer way is to open the directory in Dired and delete from there.

s (org-attach-set-directory)

Set a specific directory as the entry's attachment directory. This works by putting the directory path into the DIR property.

S (org-attach-unset-directory)

Remove the attachment directory. This command removes the DIR property and asks the user to either move content inside that folder, if an ID property is set, delete the content, or to leave the attachment directory as is but no longer attached to the heading.

Attachment options

There are a couple of options for attachments that are worth mentioning.

org-attach-id-dir

The directory where attachments are stored when ID is used as method.

org-attach-dir-relative

When setting the DIR property on a node using C-c C-a s (org-attach-set-directory), absolute links are entered by default. This option changes that to relative links.

org-attach-use-inheritance

By default folders attached to a heading are inherited from parents according to org-use-property-inheritance. If one instead want to set inheritance specifically for Org attach that can be done using org-attach-use-inheritance. Inheriting documents through the node hierarchy makes a lot of sense in most cases. Especially when using attachment links (see Attachment links). The following example shows one use case for attachment inheritance:

* Chapter A ...
  :PROPERTIES:
  :DIR: Chapter A/
  :END:
** Introduction
Some text

#+NAME: Image 1
[[attachment:image 1.jpg]]

Without inheritance one would not be able to resolve the link to image 1.jpg, since the link is inside a sub-heading to Chapter A.

Inheritance works the same way for both ID and DIR property. If both properties are defined on the same headline then DIR takes precedence. This is also true if inheritance is enabled. If DIR is inherited from a parent node in the outline, that property still takes precedence over an ID property defined on the node itself.

org-attach-method

When attaching files using the dispatcher C-c C-a it defaults to copying files. The behavior can be changed by customizing org-attach-method. Options are Copy, Move/Rename, Hard link or Symbolic link.

org-attach-preferred-new-method

This customization lets you choose the default way to attach to nodes without existing ID and DIR property. It defaults to id but can also be set to dir, ask or nil.

org-attach-archive-delete

Configure this to determine if attachments should be deleted or not when a subtree that has attachments is archived.

org-attach-auto-tag

When attaching files to a heading it will be assigned a tag according to what is set here.

org-attach-id-to-path-function-list

When ID is used for attachments, the ID is parsed into a part of a directory-path. See org-attach-id-uuid-folder-format for the default function. Define a new one and add it as first element in org-attach-id-to-path-function-list if you want the folder structure in any other way. All functions in this list will be tried when resolving existing ID's into paths, to maintain backward compatibility with existing folders in your system.

org-attach-store-link-p

Stores a link to the file that is being attached. The link is stored in org-stored-links for later insertion with C-c C-l (see Handling Links). Depending on what option is set in org-attach-store-link-p, the link is stored to either the original location as a file link, the attachment location as an attachment link or to the attachment location as a file link.

org-attach-commands

List of all commands used in the attach dispatcher.

org-attach-expert

Do not show the splash buffer with the attach dispatcher when org-attach-expert is set to non-nil.

See customization group Org Attach if you want to change the default settings.

Automatic version-control with Git

If the directory attached to a heading is a Git repository, Org can be configured to automatically commit changes to that repository when it sees them.

To make Org mode take care of versioning of attachments for you, add the following to your Emacs config:

(require 'org-attach-git)

Attach from Dired

It is possible to attach files to a subtree from a Dired buffer. To use this feature, have one window in Dired mode containing the file(s) to be attached and another window with point in the subtree that shall get the attachments. In the Dired window, with point on a file, M-x org-attach-dired-to-subtree attaches the file to the subtree using the attachment method set by variable org-attach-method. When files are marked in the Dired window then all marked files get attached.

Add the following lines to the Emacs init file to have C-c C-x a attach files in Dired buffers.

(add-hook 'dired-mode-hook
          (lambda ()
            (define-key dired-mode-map
              (kbd "C-c C-x a")
              #'org-attach-dired-to-subtree)))

The following code shows how to bind the previous command with a specific attachment method.

(add-hook 'dired-mode-hook
          (lambda ()
            (define-key dired-mode-map (kbd "C-c C-x c")
              (lambda ()
                (interactive)
                (let ((org-attach-method 'cp))
                  (call-interactively #'org-attach-dired-to-subtree))))))

RSS Feeds

Org can add and change entries based on information found in RSS feeds and Atom feeds. You could use this to make a task out of each new podcast in a podcast feed. Or you could use a phone-based note-creating service on the web to import tasks into Org. To access feeds, configure the variable org-feed-alist. The docstring of this variable has detailed information. With the following

(setq org-feed-alist
      '(("Slashdot"
         "https://rss.slashdot.org/Slashdot/slashdot"
         "~/txt/org/feeds.org" "Slashdot Entries")))

new items from the feed provided by rss.slashdot.org result in new entries in the file ~/org/feeds.org under the heading Slashdot Entries, whenever the following command is used:

C-c C-x g (org-feed-update-all)

Collect items from the feeds configured in org-feed-alist and act upon them.

C-c C-x G (org-feed-goto-inbox)

Prompt for a feed name and go to the inbox configured for this feed.

Under the same headline, Org creates a drawer FEEDSTATUS in which it stores information about the status of items in the feed, to avoid adding the same item several times.

For more information, including how to read atom feeds, see org-feed.el and the docstring of org-feed-alist.

Footnotes

  1. 30

    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 using file+olp+datetree since the older targets are now deprecated.

    Backrefs: 1

  2. 31

    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.

    * 2022
    ** 2022-10 October
    *** 2022-10-07 Friday
    *** 2022-10-08 Saturday
    

    TODO state, priority, tags, statistics cookies, and COMMENT keywords are allowed in the tree structure.

    Backrefs: 1

  3. 32

    This is always the other, not the user. See the variable org-link-from-user-regexp.

    Backrefs: 1

Manual
Org Mode 9.8.5
Source
View upstream