The Lumber Room

"Consign them to dust and damp by way of preserving them"

Emacs copy/paste and X

with 17 comments

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:

Clipboard
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.
Others
[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 general picture is explained at jwz’s page and in this spec.

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:

  1. 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.

  2. There is mouse-sel-mode, which seems to affect pasting into emacs, whatever the name might say.
  3. 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.

  4. 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.
  5. 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.)

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.

Written by S

Sun, 2006-10-22 at 23:55:00

Posted in compknow

17 Responses

Subscribe to comments with RSS.

  1. Thanks for posting this. I’ve been struggling with this since I’ve been using Emacs over the past couple of years. My solution has just been to save the file in Emacs and cat it out in Gnome Terminal and copy it from there. -Not an ideal solution. If you figure it out, let me know and I’ll do the same.

    Andy

    Sun, 2006-11-05 at 22:48:23

  2. http://groups.google.com/group/comp.emacs/browse_thread/thread/eacac402db7b2426/c32f8137c01555d4%23c32f8137c01555d4

    Bjoern Milcke’s solution in the above link works for me. Copy or cut normally from Emacs and use the middle button to paste in X – at least it works in Gnome Terminal on Ubuntu. If you don’t have a middle button, I think clicking both left/right buttons together works as a middle click, too.

    Andy

    Sun, 2006-11-05 at 23:16:49

  3. Actually, I don’t know… it seems to be working fine right now here too, but I know there have been times when it hasn’t worked. I’ve tried everything as simple as what you suggest, but it hasn’t worked for me. I think I’ll post here again when it doesn’t work (I use Debian at two places and Ubuntu at home; I’ll post again from elsewhere, I’m sure).
    BTW your first solution was already mentioned in my post, with gnome-terminal replaced with konsole (I’m usually on KDE) :-)

    shreevatsa

    Mon, 2006-11-06 at 15:07:02

  4. I feel your pain!

    Using emacs on different computers, the solutions people suggest sometimes work – and sometimes they don’t, and I can’t work out why.

    Then you get into pasting anything other than text from a european language. Find anything written in cyrillic, paste it into Emacs, and watch as everything breaks. Meh!

    Dan

    Sun, 2006-11-12 at 05:26:11

  5. I have this problem only with copying from emacs that are opened from remote machines. Locally, I can copy and paste among emacs and programs fine.

    I tried adding those two .emacs lines above, to no avail.

    Any ideas?

    vince

    Tue, 2007-03-13 at 05:05:19

  6. I’ve never found a solution to this problem either. I’m using Emacs 22.1.1 inside Konsole with KDE 3.5.8 (though its been a problem with earlier versions too).

    I can paste from X into Emacs OK by taking advantage of the fact that Konsole can be pasted into. However, Emacs interprets this as the text having been typed which is sometimes significant (e.g. with undo).

    But I can’t copy from Emacs into the X clipboard. I’ve tried setting:

    (setq x-select-enable-clipboard t)
    (global-set-key [(shift delete)] ‘clipboard-kill-region)
    (global-set-key [(control insert)] ‘clipboard-kill-ring-save)
    (global-set-key [(shift insert)] ‘clipboard-yank)

    and various options for interprogram-paste-function and interprogram-cut-function most of which tend to stop even normal yanking and killing from working! I’ve also tried both with and without Klipper. The only thing I can do is use Konsole’s copy function however this has some serious drawbacks: it requires that I select text with the mouse; it doesn’t handle lines which Emacs has wrapped correctly (it interprets them as being multiple lines ending with “/”); and of course you can’t select more text from a buffer than you can fit in a window.

    It would be great to get this working.

    Richard Lewis

    Fri, 2007-10-26 at 16:52:27

  7. Not sure if you found a solution to this yet, but try M-x clipboard-kill-region. This should kill what you want to copy and save it to the clipboard. If you then hit undo (C-/) you will get the desired effect.

    Chris

    Sat, 2008-05-31 at 18:43:58

  8. After spending months just putting up with not having this feature, I wrote a quick hack just now:

    (defun copy-to-x-clipboard ()
    (interactive)
    (with-current-buffer (current-buffer)
    (call-process-region (region-beginning) (region-end) “xsel” nil 0 nil “-p” “-i”)))
    (global-set-key [(control insert)] ‘copy-to-x-clipboard)

    I’m a bit of an Elisp n00b so there’re probably various inelegantcies. It uses xsel to put the current region into the primary selection. Tested with emacs 22 (nox) in urxvt on Fluxbox. I’m not using KDE anymore, but if Klipper has a CLI, maybe another call-process-region call could be used. Have a look at M-x man RET xsel RET to check for options for things like copying to the X clipboard rather than primary selection.

    Richard Lewis

    Tue, 2008-07-01 at 13:10:03

  9. Haha, using xsel is a brilliant idea :) I’m sure it will work under X everywhere, too.

    Shreevatsa

    Tue, 2008-07-01 at 18:04:27

  10. Thanks for that! I sort of “completed” this a bit, see http://hugoheden.wordpress.com/2009/03/08/copypaste-with-emacs-in-terminal/

    hugoheden

    Sun, 2009-03-08 at 19:04:47

  11. […] Posted by hugoheden on March 8, 2009 I often run emacs in a terminal window. This is a way to get copy-paste “working” to and from other applications. Note that it requires xsel, “a command-line program for getting and setting the contents of the X selection”. See also https://shreevatsa.wordpress.com/2006/10/22/emacs-copypaste-and-x/ […]

  12. hugoheden: You’re welcome, and thanks! I’ll come back and update this post when I get the time.

    Shreevatsa

    Tue, 2009-03-10 at 16:23:42

  13. When copying from emac to Window I just use Ctrl+c and that always works for me

    Anonymous

    Sun, 2009-07-12 at 16:30:16

    • You’re lucky, then. :-) [Or Emacs has improved; note that this post was originally written in 2006.]

      Shreevatsa

      Sun, 2009-07-12 at 19:55:32

  14. […] Emacs copy/paste and X « The Lumber Room i have this problem pretty continually. pain in the ass. (tags: emacs copypaste x linux bug) […]

  15. I tried some of the above initially without success, but then found a way to paste from X to emacs 21.4.1 under rhe5 by using
    (setq x-select-enable-primary t)
    and it also works from emacs to X.

    Paste works with middle mouse button, and scroll works now I have (mwheel-install).

    nqwerty

    Thu, 2010-12-16 at 13:47:43

  16. Hello
    These lines work for me (Terminal, Firefox, …) :
    (setq x-select-enable-clipboard t)
    (setq interprogram-paste-function ‘x-cut-buffer-or-selection-value)

    I’m under Gnome2 and emacs 23.3.1

    Thanks for your help ! :)
    Flo

    Azerty

    Tue, 2012-06-05 at 19:59:01


Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.