sligocki
sligocki

Reputation: 6387

Integrate Emacs copy/paste with System copy/paste

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?

Upvotes: 32

Views: 13919

Answers (5)

davidA
davidA

Reputation: 13664

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.

Upvotes: 0

mcandre
mcandre

Reputation: 24602

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))))

Upvotes: 12

Nathan Geffen
Nathan Geffen

Reputation: 439

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

Upvotes: 27

danlei
danlei

Reputation: 14291

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

Upvotes: 4

jamessan
jamessan

Reputation: 42667

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

Upvotes: 14

Related Questions