org-to-clipboard

HomeAbout
Thu 30 Apr 2020

I used to be one of those people who insisted that email messages should be pure text, with no markup at all. After all, who sends the most elaborately formatted messages? Spammers do. But that's not a good argument; it's guilt by association. Eventually, I came around to the idea that using bulleted lists, hyperlinks, indentation, and the occasional bold and italic text can make messages easier to read.

For years, I've wanted to be able to compose HTML messages in Emacs. I've tried countless tools to make that work, from Atomic Chrome to Mu4e. I never found something that worked properly for markup. Finally, a few days ago, I figured out a nearly trivial hack that makes it easy. The idea is to edit messages using Emacs Org mode, then copy them into the Gmail composition window as HTML.

If you'd like to try it, start by installing org2clip somewhere on your path. Here's the Linux version, which uses Pandoc to convert text from Org to HTML, then xclip to store the HTML in the X11 clipboard:

#!/bin/bash
pandoc --from=org --to=html \
| xclip -selection clipboard -target text/html \
&> /dev/null

If you're running MacOS, install this version of org2clip, which is based on a trick from Stack Overflow for setting the contents of the clipboard:

#!/bin/bash
HEX=`pandoc --from=org --to=html | hexdump -ve '1/1 "%.2x"'`
osascript -e "set the clipboard to «data HTML${HEX}»"

Next, install the Emacs command org-to-clipboard:

(defun org-to-clipboard ()
  "Convert the contents of the current buffer or region from Org
mode to HTML.   Store the result in the clipboard."
  (interactive)
  (if (use-region-p)
      (shell-command-on-region (region-beginning)
                               (region-end)
                               "org2clip")
      (shell-command-on-region (point-min)
                               (point-max)
                               "org2clip")))

To compose a message, write it in an Org mode buffer. When you're ready to send it, do M-x org-to-clipboard RET, then paste into the Gmail composition window.

Now that I have this tool, I use it constantly. In my work as editor of SRFI (the Scheme Requests for Implementation), I use it in combination with Yasnippet to produce standardized announcements, which often include links and bulleted lists of Git commit summaries. And it works not only for email, but also for things like Reddit r/scheme posts.

Note that it would be trivial to change org2clip and org-to-clipboard to use another markup language like Markdown; just edit the --from argument.

Now I just need one more thing for perfect email composition: a way to edit replies, preserving formatting. If you have a practical solution, please let me know.

Addendum

I linked to this post on Reddit's /r/orgmode and got many interesting and useful comments.