How to copy text from Emacs to another application on Linux

后端 未结 13 2304
被撕碎了的回忆
被撕碎了的回忆 2020-12-07 07:29

When I cut (kill) text in Emacs 22.1.1 (in its own window on X, in KDE, on Kubuntu), I can\'t paste (yank) it in any other application.

13条回答
  •  不思量自难忘°
    2020-12-07 08:11

    The code below, inspired by @RussellStewart's answer above, adds support for x-PRIMARY and x-SECONDARY, replaces region-active-p with use-region-p to cover the case of an empty region, does not return silently if xsel has not been installed (returns an error message), and includes a "cut" function (emacs C-y, windows C-x).

    (defun my-copy-to-xclipboard(arg)
      (interactive "P")
      (cond
        ((not (use-region-p))
          (message "Nothing to yank to X-clipboard"))
        ((and (not (display-graphic-p))
             (/= 0 (shell-command-on-region
                     (region-beginning) (region-end) "xsel -i -b")))
          (error "Is program `xsel' installed?"))
        (t
          (when (display-graphic-p)
            (call-interactively 'clipboard-kill-ring-save))
          (message "Yanked region to X-clipboard")
          (when arg
            (kill-region  (region-beginning) (region-end)))
          (deactivate-mark))))
    
    (defun my-cut-to-xclipboard()
      (interactive)
      (my-copy-to-xclipboard t))
    
    (defun my-paste-from-xclipboard()
      "Uses shell command `xsel -o' to paste from x-clipboard. With
    one prefix arg, pastes from X-PRIMARY, and with two prefix args,
    pastes from X-SECONDARY."
      (interactive)
      (if (display-graphic-p)
        (clipboard-yank)
       (let*
         ((opt (prefix-numeric-value current-prefix-arg))
          (opt (cond
           ((=  1 opt) "b")
           ((=  4 opt) "p")
           ((= 16 opt) "s"))))
        (insert (shell-command-to-string (concat "xsel -o -" opt))))))
    
    (global-set-key (kbd "C-c C-w") 'my-cut-to-xclipboard)
    (global-set-key (kbd "C-c M-w") 'my-copy-to-xclipboard)
    (global-set-key (kbd "C-c C-y") 'my-paste-from-xclipboard)
    

提交回复
热议问题