How to copy text from Emacs to another application on Linux

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

    I assume by emacs you are meaning Emacs under X (ie not inside a terminal window).

    There are two ways:

    1. (Applies to unix OS's only) Highlight the desired text with your mouse (this copies it to the X clipboard) and then middle click to paste.
    2. Highlight the desired text and then "M-x clipboard-kill-ring-save" (note you can bind this to an easier key). Then just "Edit->Paste" in your favorite app.

    Clipboard operations available:

    • clipboard-kill-ring-save -- copy selection from Emacs to clipboard
    • clipboard-kill-region -- cut selection from Emacs to clipboard
    • clipboard-yank -- paste from clipboard to Emacs
    0 讨论(0)
  • 2020-12-07 08:01

    I always use quick paste -- drag selection in emacs, hit the middle mouse button in target window.

    (From the reference to kate, I take it you're on linux or similar and probably using emacs in X one way or another.)

    0 讨论(0)
  • 2020-12-07 08:08

    You might want to specify what platform you are using. Is it on linux, unix, macosx, windows, ms-dos?

    I believe that for windows it should work. For MacOSX it will get added to the x-windows clipboard, which isn't the same thing as the macosx clipboard. For Linux, it depends on your flavour of window manager, but I believe that x-windows handles it in a nice way on most of them.

    So, please specify.

    0 讨论(0)
  • 2020-12-07 08:11

    There is an EmacsWiki article that explains some issues with copy & pasting under X and how to configure it to work.

    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 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)))
    
    0 讨论(0)
提交回复
热议问题