I often want to create a link to the attachment I've just added. Especially when the attachment is an image and I want to show the image inline.
What I usually do is I open the attachment folder in dired mode, copy the filename, then create a link manually by writing [[attachment:the-file-name]].
I was just wondering if there's a quicker way to do this?

  • Have you looked at Attach from Dired in the manual?
    – NickD
    Aug 28, 2020 at 15:24
  • @NickD that’s a different/quicker way to create an attachment. I’m interested in knowing if there’s a quicker way to link to it afterwards. Aug 28, 2020 at 15:34

2 Answers 2


The variable org-attach-store-link-p is specifically designed for this purpose.
Adding the following line to your init file would make org push a [[attachment:file-name.ext]] type link to org-stored-links every time you add an attachment.
So after adding an attachment you can just use C-c C-l to insert the link.

(setq org-attach-store-link-p 'attached)
  • 1
    I think you should unaccept my answer and accept yours. It is clearly the right answer.
    – NickD
    Aug 29, 2020 at 5:04
  • I spent some time wondering about this variable because, for once, it's documentation is not complete. You have to look at the variable definition in the source code to get the accepted values which are: attached file or t
    – skizo
    Feb 11, 2022 at 10:16

The function org-attach-dired-to-subtree creates attachments based on the marked files (or the file the cursor is on) in a dired buffer - see Attach from Dired for details, including how to bind it to a key sequence (C-c C-x a which happens to be unused by default in dired-mode).

The function org-attach-dired-to-subtree-store-links below is almost exactly a copy of org-attach-dired-to-subtree, except that instead of making attachments, it creates links that can be inserted into the Org mode buffer using org-insert-link (usually bound to C-C C-l if you follow the convention in the Org mode manual).

The description of the link is the filename of the attachment, but you can change that when you insert the link. The code also shows a binding for that function (C-c C-x l which also happens to be unused by default in dired-mode):

* foo                                                                                                           :ATTACH:
:ID:       7df0d4a8-1cd0-4f7b-84ec-e6b4dba6aa24


* Code

#+begin_src emacs-lisp
  (defun org-attach-dired-to-subtree-store-links (files)
     (list (dired-get-marked-files)))
    (unless (eq major-mode 'dired-mode)
      (user-error "This command must be triggered in a dired buffer"))
    (let ((start-win (selected-window))
            (lambda (window)
              (with-current-buffer (window-buffer window)
                (eq major-mode 'org-mode))))))
      (unless other-win
         "Can't attach to subtree.  No window displaying an Org buffer"))
      (select-window other-win)
      (dolist (file files)
        (let* ((fname (file-name-nondirectory file))
               (link (format "attachment:%s" fname))
               (desc fname))
          (push (list link desc) org-stored-links)))
      (select-window start-win)))

#+begin_src emacs-lisp
  (lambda ()
    (define-key dired-mode-map (kbd "C-c C-x l") #'org-attach-dired-to-subtree-store-links))))

Assuming that you've done both keybindings as above, the workflow is:

  • An Org mode buffer in one window
  • A dired buffer in a second window
  • Mark all the files that you want to attach
  • C-c C-x a - that attaches all the marked files
  • C-c C-x l - that stores attachment links to all the marked files
  • Back in the Org mode buffer, do C-c C-l to insert each link.
  • Probably overwriting org-attach-dired-to-subtree is better? Saves a step. Aug 29, 2020 at 1:01
  • 1
    It doesn't have to be this complicated. It can be done by simply setting the org-attach-store-link-p variable to 'attached Aug 29, 2020 at 4:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.