Emacs copy/paste and X
Many people have problems getting copy-paste working in Emacs. Sometimes this can be ascribed to a misunderstanding of how the system works, but often it is a bug with Emacs itself.
First, a general word about how cut/copy/paste work in Linux and all applications:
Much of the confusion arises because there are actually two mechanisms for copying text between applications:
- There is an explicit “clipboard”, just as in Windows. If you select some text, and explicitly click “copy” (or “cut”) somewhere (that is, either in the right-click menu or in the Edit menu, usually), it goes into the clipboard. You can paste it elsewhere by again explicitly clicking on “paste” (or Ctrl-V, often). This works perfectly well within itself, and just as in Windows, so there are no problems at all (except in Emacs sometimes, and we’ll come to that).
- Primary selection
- In addition, there is the “primary selection”. Consider it an additional feature which you can ignore if you like. It works like this: when you select some text (just select… highlight it, that’s all), it gets automatically “copied”, and you can paste it into another location simply by middle-clicking. This is intended for more temporary tasks, and shouldn’t be used for anything serious. A thing to watch out for is that unlike in the case of the clipboard, the text doesn’t really get copied anywhere. What actually happens when you middle-click to paste, is that the destination program asks the source program the text to be pasted. A consequence of this is that if you select some text and close the source program, you can’t paste that text anymore. In short, a good way to think of this is as “extended drag-and-drop“, in which you are allowed to let go of the mouse, change windows, and maybe even do a little bit of work (which does not require selecting) in some another window before finally “dropping” with the middle-mouse click.
- [There is also a “secondary selection” and another ancient mechanism involving “cut buffers”, but neither of them is in use these days. Clipboard managers, like Klipper or the GNOME clipboard tool, can often be asked to use either the clipboard alone or the primary selection too.]
- This page claims to have a fix, which is essentially to add the lines
(setq x-select-enable-clipboard t) (setq interprogram-paste-function 'x-cut-buffer-or-selection-value)
to ~/.emacs. It didn’t help.
- There is mouse-sel-mode, which seems to affect pasting into emacs, whatever the name might say.
- The emacs manual says
To copy text to another X window, kill it or save it in the kill ring. Under X, this also sets the primary selection. Then use the “paste” or “yank” command of the program operating the other window to insert the text from the selection.
, but it didn’t help me either.
- I’ve discovered that the situation is better when Klipper is running — selecting (or copying) text in Emacs results in it being copied (imperfectly) to Klipper, and I can then paste in other apps, provided I first click on Klipper and choose that text. Weird.
- I can select in Emacs, and middle-click it onto konsole. It works. I can then select that and paste it into Firefox. That works too. So maybe Emacs and Firefox don’t have a good way of talking to each other. (The same seems to be true of any GNOME app, not just Firefox.)
Second, the problem: Somehow, Emacs has problems with this. It is very inconsistent, so it’s hard to pin down the exact nature of the problem, but in general copying text between other applications and Emacs seems to just not work sometimes. With me, pasting into emacs works (almost always) by default; copying from emacs doesn’t, no matter what I try. Others have had the opposite problem.
Some possible solutions/ideas:
The bug is clearly somewhere under x-set-selection, because invoking it directly doesn’t work either.
And, trying to set the Emacs font today, I discovered another bug: I couldn’t paste from the xfontsel selection to any KDE app. I could do the thing of pasting into Firefox and then pasting into Emacs, though. There is also this discussion about what happens (or should happen) when something is “copied” without being selected first.
There are some other discussions at http://natalian.org/archives/2006/04/02/copy-and-paste/ , http://berserk.org/2006/04/04/copy-or-cut-and-paste-firefox-bug/ , and this Slashdot article about copy-paste, although none of it has been useful.
Please leave comments here if you have something to say; I still haven’t managed to find a workaround.
Updates: I found that M-x clipboard-kill-ring-save and M-x clipboard-yank seem to work for copying from Emacs to the clipboard and pasting from the clipboard into Emacs. In some of the comments below, Richard Lewis had the idea of using xsel, and Hugo Heden completed it further.