Emacs command to cut OR copy to system clipboard?

6,878

Solution 1

On Linux, you can use xclip.el (https://www.emacswiki.org/emacs/xclip.el) to link the kill-ring to the clipboard from Emacs running in terminal (without losing the multiple-kill and yank capability).

To install: M-x package-install RET xclip RET. To use, just add (xclip-mode 1) to your ~/.emacs.


This didn't exist on Mac, so I modified xclip.el to work with the OSX pbcopy and pbpaste utilities. The result, pbcopy.el, is available here: https://gist.github.com/1023272

Solution 2

You must be talking about X clipboard, so put this line into your .emacs file:

(custom-set-variables '(x-select-enable-clipboard t))

Solution 3

When emacs is running under X, there are a number of ways to implement this (if I understand you correctly). If you have a recent version of emacs, I think the following lines in .emacs creates the most intuitive behavior:

(global-set-key "\C-w" 'clipboard-kill-region)
(global-set-key "\M-w" 'clipboard-kill-ring-save)
(global-set-key "\C-y" 'clipboard-yank)

As always, the poorly organized but informative EmacsWiki has information on this issue.

However, when you are running in a terminal these settings (and the others mentioned so far) won't help you since the terminal is sitting between emacs and X. How to copy text from the terminal will usually be terminal dependent.

Solution 4

In CarbonEmacs (a native look-n-feel build for Mac OS X) copy and paste between command-keys in native apps, middle-click in X and C-[w,y] in emacs just work.

Share:
6,878
hpy
Author by

hpy

Updated on September 17, 2022

Comments

  • hpy
    hpy over 1 year

    As a very new Emacs user, I've been using Ctrl+w and Ctrl+y to cut/copy and paste between buffers in Emacs. Is there a way to do the same thing so that not just Emacs, but the entire system, can have access to what I am copying and pasting? Thanks.

    P.S. I would like to do this in Linux (in a terminal emulator running under X) and Mac OS X (e.g. with Terminal).

    • Admin
      Admin over 13 years
      You should probably say if you are you running emacs in a terminal, in X, or some other way (say, CarbonEmacs on a mac), as it may make a difference.
    • Admin
      Admin over 13 years
      I am running Emacs in a X terminal, either in xterm in Linux, or Terminal in Mac OS X. Will update the question.
  • hpy
    hpy over 13 years
    Thanks! Is what I am looking for, but when I tried it in Mac OS X Terminal running Emacs, the text that gets C-w in Emacs does not appear "pastable" in other Mac OS X applications. Is there more I should do? Thanks!
  • Steven D
    Steven D over 13 years
    Unfortunately, when you are running emacs from the terminal, none of the solutions presented here will do much for you as far as I know.
  • JRobert
    JRobert almost 13 years
    Coming in late to say: "Maybe". I'm seeing unix newlines translated to lone carriage-returns, when copying/cutting in Emacs and looking at the result in the OS-X clipboard. Question and more details at superuser.com/questions/293656
  • alper
    alper almost 4 years
    To install: M-x package-install RET xclip RET. To use, just add (xclip-mode 1) to your ~/.emacs
  • alper
    alper almost 4 years
    Along with this should I also enable (xclip-mode 1) ?
  • alper
    alper almost 4 years
    I tried all clipboard-* but seems like it did not able to paste the copied text from emacs (on a remote machine) to remote machine's shell