Integrate Emacs copy/paste with System copy/paste

后端 未结 5 1739
星月不相逢
星月不相逢 2020-12-24 13:18

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 sh

相关标签:
5条回答
  • 2020-12-24 13:36

    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.

    0 讨论(0)
  • 2020-12-24 13:40

    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

    0 讨论(0)
  • 2020-12-24 13:45

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

    0 讨论(0)
  • 2020-12-24 13:49

    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))))
    
    0 讨论(0)
  • 2020-12-24 13:58

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

    0 讨论(0)
提交回复
热议问题