There's lots of documentation about inserting or storing org-mode-links, but apparently none about copying them to buffers in other formats.

How do you copy the URL of an org-mode-link from an org file into the clipboard/kill ring with as few keystrokes as possible? I tried org-store-link and org-insert-link, but it dumps the whole org-mode syntax into whatever other buffer you have open.

Example: buffer 1 org-mode, buffer 2 Markdown.

  1. Visit buffer 1, C-s search for link.
  2. Magic happens, URL is in clipboard
  3. Visit buffer 2. C-y to yank URL into buffer.
up vote 10 down vote accepted

Here is one way of achieving what you want, using text properties.

You can go to the link in the org file you want to copy and execute the command my-org-retrieve-url-from-point this will copy the org link at current point to the clipboard. Before adding the link to clipboard a yank-handler (my-yank-org-link) is registered which is called while pasting the link. The yank-handler checks if the current buffer is in org-mode or a mode derived from org-mode, if so it inserts the link as it is (a org-link), otherwise it extracts the URL from the link and inserts it

(defun my-yank-org-link (text)
  (if (derived-mode-p 'org-mode)
      (insert text)
    (string-match org-bracket-link-regexp text)
    (insert (substring text (match-beginning 1) (match-end 1)))))

(defun my-org-retrieve-url-from-point ()
  (interactive)
  (let* ((link-info (assoc :link (org-context)))
         (text (when link-info
                 ;; org-context seems to return nil if the current element
                 ;; starts at buffer-start or ends at buffer-end
                 (buffer-substring-no-properties (or (cadr link-info) (point-min))
                                                 (or (caddr link-info) (point-max))))))
    (if (not text)
        (error "Not in org link")
      (add-text-properties 0 (length text) '(yank-handler (my-yank-org-link)) text)
      (kill-new text))))

Here is a DWIM version of kill-ring-save, which uses kill-ring-save if region is active or copies the org-link at point

(defun my-smarter-kill-ring-save ()
  (interactive)
  (if (region-active-p)
      (call-interactively #'kill-ring-save)
    (when (eq major-mode 'org-mode)
      (call-interactively #'my-org-retrieve-url-from-point))))

Note: This does not currently distinguish between a URL link and internal links.

  • 1
    I removed the if, because when using the GUI Emacs it always copied the entire link ([[a]][b]]) instead of the URL part (a) – Nitz Feb 1 '15 at 8:06

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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