Quick tip for Linux users having trouble with Emacs (or XEmacs) copy/paste

by Uche Ogbuji

Related link: http://www.emacswiki.org/cgi-bin/wiki/CopyAndPaste

The above link probably has enough information in disjointed form for you to cobble together a working solution for you, but in a nutshell what worked for me with GNU Emacs 21.3-17 in Fedora Core 3 was adding the following to my .emacs::

(setq x-select-enable-clipboard t)
(setq interprogram-paste-function 'x-cut-buffer-or-selection-value)

Why these settings aren't the default on Linux completely bewilders me. How does anyone conceive that the average user will not want reliable cut and paste between their editor and other apps? Certainly most users will not be willing or able to go through what I did to get this sorted out.

The problem that I was seeing was that sometimes middle mouse click would work for cut and paste, and sometimes it wouldn't. It was very erratic, even within the same emacs session. Every now and then using keyboard rather than mouse, or using menu, or some other random variation would seem to help ephemerally. I've seen numerous reports from other users with this same problem on emacs lists and elsewhere, and I almost invariably see either no response, or some comment from from emacs gurus completely discounting the poor user's problems. Clearly this is a widespread problem that needs some attention in the Emacs communities.

I had never been able to use the typical GNOME clipboard operations at all in the past (e.g. yank from emacs then Ctrl-V to Firefox/Mozilla/Gedit or Shift-Ctrl-V to gnome-terminal). I've similar reports from KDE users.

With the above lines in my .emacs, cut/copy/paste now seems reliable with middle mouse, and I can indeed use GNOME clipboard ops as expected in GNU Emacs. In XEmacs (21.4.15) paste to XEmacs works fine from clipboard, though explicit clipboard events do not seem to work from XEmacs to other apps. Middle mouse click does work better, so that's good enough for me. Besides, I've migrated from XEmacs to GNU Emacs because XEmacs does not support nXML, James Clark's superlative XML mode. If you do use XEmacs the top link includes some suggested variations for XEmacs users.

Overall, I hope that XEmacs, GNU Emacs and their various distributors work out the kinks so that such a simple matter as cut and paste is not a matter of medieval alchemical incantations. Until then, I hope my notes are of help to someone else.


2005-01-06 12:51:26
Thanks much
That has been bugging me for months and I never got around to working on it. Worked like a charm on Debian unstable with Emacs 21.3.1 under PowerPC, kernel 2.6.10.
2005-01-07 01:45:15
An alternative
Thanks. Your suggestion works well, but there's one
problem. The middle mouse button is supposed (I think) to paste the primary selection, whether Emacs
or another app owns it. Using your settings, I always get the last primary selection set by Emacs.

The compromise I've settled on is to treat destop
cut/copy/paste as different operations from Emacs kill/yank. Ideally, I would have used Ctrl-X/Ctrl-C/Ctrl-V for these, but that obviously
interferes with Emacs. So here's what I do instead:

(global-set-key "\C-cc\C-c" 'clipboard-kill-ring-save)
(global-set-key "\C-cc\C-x" 'clipboard-kill-region)
(global-set-key "\C-cc\C-v" 'clipboard-yank)

2005-01-07 05:48:57
Cut and not paste
I was just as curios/cross Uche.
I brought up other editors for cut and paste,
thinking it was FC3 specific.

Much appreciated.


ps. You should buy the new rev 3 emacs book,
its definately worth a read.

2005-05-15 07:26:51
Thank You!
I recently switched from Windows "NT" Gnu Emacs to Linux Gnu Emacs, and the lack of copy-paste from Emacs to other apps had been really frustrating. I found that with Emacs 21.3.1 (on CentOS 4, kernel 2.6.9), all I needed was:(setq x-select-enable-clipboard t)The other setting was already correct when I checked it in *scratch*.
2006-06-09 23:58:10
Uche's tip appears only to work from application that understand C-c
for copy. For example, text selected by mouse-left-sweep in an xterm
does not reliably transfer, whereas text selected by mouse-left-sweep
and then C-c works (example: firefox can do this, xterm cannot).

Does anyone know a fix for this?