Saturday, November 18, 2006

Cutting and Pasting with X Windows on the Mac

If you are a hardcore X windows person like me, working on a Mac presents certain issues. Mac OSX applications hosted by X windows are more or less second class citizens. Most X windows people are used to copying directly to the clipboard by selecting, and pasting with the middle mouse button. However, on the Mac, the behavior not really consistent.
  • For clipboard contents created by non-X applications, pasting into X with the middle button works fine.
  • After selecting something within X, pasting back into X with the middle button works fine as well.
  • However, when you want to copy from X to a non-X application, then you need to go through the extra step of invoking "Copy" from the "Edit" menu. This is especially frustrating when you want to copy a URL from an Xterm to Firefox.
What I really wanted was "just" a way to automatically copy the X selection to the Mac clipboard. I puzzled over complicated descriptions such as these, until I finally just did some experimentation. It turns out it can be done!

Just edit the .Xresources file in your home directory (or create a file with that name), and add the following to it.
XTerm*VT100.Translations: #override \
<btn1up>: select-end(SELECT, CLIPBOARD, PRIMARY, CUT_BUFFER0)\n\
Ctrl <keypress> Insert:insert-selection(PRIMARY, CUT_BUFFER0)
Then you should restart X (or at least "xrdb -merge ~/.Xresources"). For emacs, you should add the following to your .emacs file,

(setq x-select-enable-clipboard t)
I've been very happy ever since.

No comments: