32

I have (cua-mode t) in my .emacs, so that C-c is copy and C-v is paste just like most other programs on my desktop (Ubuntu, Gnome, Linux). However, Emacs does not seem to share the clipboard/copy buffer with other programs.

For example, if I C-c in Firefox I can S-C-v to paste into a terminal or C-v to paste into gedit. However, if I C-v (or C-y) in emacs, I do not get what I copied from Firefox.

Is there any way to make this work? Is there another command I can use to access the system's copy-paste buffer?

5
  • It does share a clipboard with system by the way, but only mouse selection (i.e. what you can paste with a middle-click elsewhere).
    – user319799
    Jul 9, 2010 at 20:03
  • Shouldn't this be on superuser.com?
    – Vitor Py
    Jul 9, 2010 at 20:06
  • 1
    Vim and Emacs questions are generally considered ok for SO as they're programmer's tools. See the various discussions that have already taken place about this on meta.
    – jamessan
    Jul 9, 2010 at 20:09
  • @jamessan Ok, I agree :)
    – Vitor Py
    Jul 9, 2010 at 20:10
  • @doublep yes it does seem to use X11 selection buffer, but it doesn't seem to use the clipboard by default. So selecting and middle click work, but copy-paste key-commands don't.
    – sligocki
    Jul 10, 2010 at 1:18

5 Answers 5

27

I had the same problem. I added this to my .emacs file:

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

Now Ctrl-C and Ctrl-v between Emacs and other applications work fine. Source: Ubuntu Forums

5
  • Ah, even better. I only had the first command. But the second helps a problem I'd had, where selecting text and middle clicking in emacs did not copy correctly. Thanks!
    – sligocki
    May 8, 2012 at 19:01
  • I am newbie. I created ~/.emacs for the first time. I added your code to ~/.emacs and it works. Thanks.
    – kenu.heo
    Mar 4, 2014 at 8:17
  • 1
    This does not seem to be working for Mac OSX; Getting this error. current-kill: Symbol's function definition is void: x-cut-buffer-or-selection-value
    – cevaris
    Nov 20, 2014 at 23:03
  • I was missing ' before both those lines when added to emacs.el Jun 1, 2015 at 9:33
  • This does not work for me using Ubuntu 16.04 and Emacs 24. Copying text outside of terminal Emacs and then pasting (or clipboard-yank) inside Emacs doesn't retrieve the right text, even with this enabled.
    – ely
    Nov 8, 2016 at 13:09
15

See clipboard-yank and clipboard-kill-region in the clipboard section of the manual.

3
  • 1
    Great, that's exactly what I was looking for. I added (x-select-enable-clipboard t) to my .emacs file and everything is working like I'd expect. Thank you.
    – sligocki
    Jul 9, 2010 at 21:20
  • 1
    Hm, actually, this isn't exactly right. This will paste from X11 selections. But it's a lot better than it was.
    – sligocki
    Jul 10, 2010 at 1:19
  • This is sometimes bound to just paste (at least in Ubuntu)
    – Hut8
    May 22, 2013 at 17:52
12

This works on my machine:

;; CUA OS copypasta even in ncurses mode
(case system-type
  ('darwin (unless window-system
             (setq interprogram-cut-function
                   (lambda (text &optional push)
                     (let* ((process-connection-type nil)
                            (pbproxy (start-process "pbcopy" "pbcopy" "/usr/bin/pbcopy")))
                       (process-send-string pbproxy text)
                       (process-send-eof pbproxy))))))
  ('gnu/linux (progn
                (setq x-select-enable-clipboard t)
                (defun xsel-cut-function (text &optional push)
                  (with-temp-buffer
                    (insert text)
                    (call-process-region (point-min) (point-max) "xsel" nil 0 nil "--clipboard" "--input")))
                (defun xsel-paste-function()

                  (let ((xsel-output (shell-command-to-string "xsel --clipboard --output")))
                    (unless (string= (car kill-ring) xsel-output)
                      xsel-output )))
                (setq interprogram-cut-function 'xsel-cut-function)
                (setq interprogram-paste-function 'xsel-paste-function))))
2
  • Nice! This works for C-c/C-x/C-v, but now middle click doesn't share with the system any more. Does middle click still work for you?
    – sligocki
    Jul 8, 2014 at 22:38
  • this only works in the emacs window mode rather than the terminal (-nw) mode, right?
    – galactica
    Jan 5, 2015 at 7:37
4

Maybe this EmacsWiki page will help, especially the section where clipboard-kill-region, clipboard-kill-ring-save, and clipboard-yank are mentioned.

2
  • Thanks @danlei, the wiki had some useful advice.
    – sligocki
    Jul 12, 2010 at 18:01
  • You're welcome. EmacsWiki is a great source for everything concerning Emacs.
    – danlei
    Jul 13, 2010 at 15:18
0

I solve this problem with autocutsel, which works with emacs and the rest of my Ubuntu system too.

  autocutsel - keep the X clipboard and the cutbuffer in sync

I use the following commands (run once, usually invoked by my window manager's "startup" mechanism, or ~/.xsession):

autocutsel -fork
autocutsel -fork -selection PRIMARY

The first instance of autocutsel does the following:

autocutsel tracks changes in the [X11] server's cutbuffer and clipboard selection. When the clipboard is changed, it updates the cutbuffer. When the cutbuffer is changed, it owns the clipboard selection. The cutbuffer and clipboard selection are always synchronized.

However there's usually a third clipboard, called PRIMARY, which the second instance of autocutsel is used to sync with the other two.

The advantages of this are that the three main clipboards are unified, so that pasting current selection via middle-click or CUA-style copy/paste with CTRL-C and CTRL-V all work together.

The main downside of this approach is that any automatic highlighting of text (such as double-clicking to highlight a word in emacs, or clicking the BlockQuote icon in a StackOverflow edit field) will obliterate your copy buffer instantly. To work around this, I use a clipboard history recorder such as glipper, which also conveniently records all clipboard content and allows me to retrieve lost clipboard contents in such circumstances. It can take a little getting-used-to and works well for me. As an alternative, you could experiment with the -pause option, which waits for some period of time before taking the selection, which may be long enough to deselect or delete any auto-selected text. I wasn't able to get results that worked well enough for me, though.

Note that this solution doesn't require any special emacs configuration, which I use with cua-mode enabled.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.