3

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?

2
  • Have you looked at Attach from Dired in the manual?
    – NickD
    Commented 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. Commented Aug 28, 2020 at 15:34

2 Answers 2

7

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)
2
  • 1
    I think you should unaccept my answer and accept yours. It is clearly the right answer.
    – NickD
    Commented 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
    Commented Feb 11, 2022 at 10:16
1

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:
:PROPERTIES:
:ID:       7df0d4a8-1cd0-4f7b-84ec-e6b4dba6aa24
:END:

[[attachment:david-vogan-E8.pdf][E8]]
[[attachment:carrying.pdf][carrying]]
[[attachment:aurifeuillian.pdf][aurifeuillian]]

* Code

#+begin_src emacs-lisp
  (defun org-attach-dired-to-subtree-store-links (files)
    (interactive
     (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))
          (other-win
           (get-window-with-predicate
            (lambda (window)
              (with-current-buffer (window-buffer window)
                (eq major-mode 'org-mode))))))
      (unless other-win
        (user-error
         "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)))
#+end_src


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

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.
2
  • Probably overwriting org-attach-dired-to-subtree is better? Saves a step. Commented 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 Commented Aug 29, 2020 at 4:33

Your Answer

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

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