9

Is there a simple way to insert an org-mode link directly from the system clipboard? I'm thinking of selecting an URL in Safari, and inserting that link directly in an org-mode document.

11

Your question isn't very specific with respect to what type of link you want and what you would consider to be "simple". Assuming that you mean a link of the following form:

[[target][description]]

the default workflow (after putting the URL in your clipboard) is:

C-c C-l C-y RET description RET

Entering a description is optional; if you don't specify one, the link will be of the form

[[target]]
  • Simple enough. It works as expected on a Windows machine, but for some reason can't copy/paste an URL from Firefox or Safari into Emacs on MacOS with Emacs 25.0.50 – Alain Nov 7 '14 at 23:13
  • @Alain I am not on MacOS so I can't test it, but you could try using x-clipboard-yank instead of yank to paste the contents of the clipboard. This command is not bound to a key by default, so you will have to call it using M-x. If that solves the problem, you can of course set up a convenient key binding for it (via (global-set-key (kbd "C-c y") 'x-clipboard-yank)). – itsjeyd Nov 7 '14 at 23:20
3

Depending on which of the two you prefer, here are two functions that check to see if there's a URL on the clipboard and, if so, insert it at point as an org-mode link. The former does it in the form of [[url]], and the latter in the form of [[url][description]] and leaves you in the description field.

(defun insert-url-as-org-link-sparse ()
  "If there's a URL on the clipboard, insert it as an org-mode
link in the form of [[url]]."
  (interactive)
  (let ((link (substring-no-properties (x-get-selection 'CLIPBOARD)))
        (url  "\\(http[s]?://\\|www\\.\\)"))
    (save-match-data
      (if (string-match url link)
          (insert (concat "[[" link "]]"))
        (error "No URL on the clipboard")))))

(defun insert-url-as-org-link-fancy ()
  "If there's a URL on the clipboard, insert it as an org-mode
link in the form of [[url][*]], and leave point at *."
  (interactive)
  (let ((link (substring-no-properties (x-get-selection 'CLIPBOARD)))
        (url  "\\(http[s]?://\\|www\\.\\)"))
    (save-match-data
      (if (string-match url link)
          (progn
            (insert (concat "[[" link "][]]"))
            (backward-char 2))
        (error "No URL on the clipboard")))))
2

I've created an emacs package that may help you with inserting org-mode links from the clipboard:

It makes an HTTP request to the URL from the clipboard and if the response contains HTML it tries to extract the title and inserts the org-mode link in this format: [[URL][extracted-title]].


For example, after copying this question's link, doing M-x org-cliplink in an org-mode buffer will insert:

[[https://emacs.stackexchange.com/q/3280][org mode - Orgmode insert link from clipboard? - Emacs Stack Exchange]]

and the link will look hyperlinked as org mode - Orgmode insert link from clipboard? - Emacs Stack Exchange in that buffer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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