12

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.

Alain
  • 363
  • 2
  • 7
  • 1
    Also, it is really nice to configure a bookmarklet to automatically insert the link using capture. See here: http://orgmode.org/worg/org-contrib/org-protocol.html – mbork Nov 08 '14 at 11:30

3 Answers3

14

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]]
itsjeyd
  • 14,666
  • 3
  • 59
  • 87
  • 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 07 '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 07 '14 at 23:20
8

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.

rexim
  • 81
  • 1
  • 1
  • Do you know of a method to use org-cliplink in order to copy all of the open tabs in a browser window to a list of links in emacs? – StackExchanger Feb 03 '23 at 08:21
4

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")))))
Kaushal Modi
  • 25,651
  • 4
  • 80
  • 183
Dan
  • 32,980
  • 7
  • 102
  • 169