Turning URLs into Org Links

Often times when doing research for a blog post I will collect a series of URLs that I could potentially use as a link in the post. Nothing new or exciting there; almost every blogger doubtlessly does the same thing.

There’s a minor annoyance though. I use Org mode to write my blog posts and while I’m still in the research phase it’s too early to turn a URL into an Org link. After all, I don’t yet know where the link will end up in the post or what the anchor text associated with it will be. Therefore when I’m actually writing the post I have to either edit the link by hand or snarf & barf the URL into Org mode’s link generation machinery.

To make the process a little easier, I wrote the following bit of Elisp. Putting the cursor on a URL and calling org-linkify will prompt for the anchor text and turn the URL into an Org link.

(defun org-linkify (label)
  "Turn a URL into an org-mode link."
  (interactive "sLabel: ")
  (let ((url (thing-at-point 'url))
        (bnds (bounds-of-thing-at-point 'url)))
    (delete-region (car bnds) (cdr bnds))
    (insert (concat "[[" url "][" label "]]"))))

With this code I can collect URLs in the Org mode file associated with the post and then either delete them or turn them into links as I write. I still have to move the URL to the proper place in the text of the post, of course, but org-linkify makes the rest of the process a bit easier.

Update: Daniel points out in the comments that Org mode already handles this use case with the normal link machinery.

This entry was posted in Programming and tagged . Bookmark the permalink.