Integrate Emacs copy/paste with System copy/paste

2019-02-01 05:48发布

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条回答
再贱就再见
2楼-- · 2019-02-01 06:12

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

查看更多
3楼-- · 2019-02-01 06:14

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

查看更多
在下西门庆
4楼-- · 2019-02-01 06:21

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

查看更多
贼婆χ
5楼-- · 2019-02-01 06:28

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))))
查看更多
ら.Afraid
6楼-- · 2019-02-01 06:33

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.

查看更多
登录 后发表回答