How to copy text from Emacs to another application on Linux

后端 未结 13 2319
被撕碎了的回忆
被撕碎了的回忆 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:13

    I use the following, based on the other answers here, to make C-x C-w and C-x C-y be copy and paste on both Mac and Linux (if someone knows the version for Windows feel free to add it). Note that on Linux you will have to install xsel and xclip with your package manager.

    ;; Commands to interact with the clipboard
    
    (defun osx-copy (beg end)
      (interactive "r")
      (call-process-region beg end  "pbcopy"))
    
    (defun osx-paste ()
      (interactive)
      (if (region-active-p) (delete-region (region-beginning) (region-end)) nil)
      (call-process "pbpaste" nil t nil))
    
    (defun linux-copy (beg end)
      (interactive "r")
      (call-process-region beg end  "xclip" nil nil nil "-selection" "c"))
    
    (defun linux-paste ()
      (interactive)
      (if (region-active-p) (delete-region (region-beginning) (region-end)) nil)
      (call-process "xsel" nil t nil "-b"))
    
    (cond
     ((string-equal system-type "darwin") ; Mac OS X
      (define-key global-map (kbd "C-x C-w") 'osx-copy)
      (define-key global-map (kbd "C-x C-y") 'osx-paste))
     ((string-equal system-type "gnu/linux") ; linux
      (define-key global-map (kbd "C-x C-w") 'linux-copy)
      (define-key global-map (kbd "C-x C-y") 'linux-paste)))
    

提交回复
热议问题