15

I am using Mac OS and emacs -nw (the terminal mode). I don't know how can I paste things (having been implemented by M-w in emacs -nw) outside the emacs.

I know that the emacs -ns can do it.

Searching the internet and the command C-h b, i find out that method, but it didn't work out.

(setq x-select-enable-clipboard t)

(setq interprogram-cut-function 'x-select-text)

I don't know much about the argument of interprogram-cut-function. Where does the x-select-text come from and what does it mean?

tshepang
  • 10,772
  • 21
  • 84
  • 127
luthur
  • 413
  • 4
  • 13

3 Answers3

30

If you are using Ubuntu 12.04 or Fedora 21, there are a couple of options to make this work.

First you need to install xclip

sudo apt-get install xclip

First Option: For Emacs 24

If you are using emacs24 you can install from the list of packages

M-x package-list-packages

Select

xclip //mine was version 1.3

In your .emacs add:

(xclip-mode 1)

Second Option. For emacs before version 24

Install xclip.el: Integrating Emacs with the X11 Clipboard in Linux

Third Option. Using @Nicholas Riley code shown in the answer

To use the code in the answer you need pbcopy / pbpaste in Ubuntu (command line clipboard)

Steve HHH
  • 12,073
  • 5
  • 64
  • 68
elviejo79
  • 4,522
  • 2
  • 29
  • 35
  • 3
    ...all of which would be highly useful in this context, except that the asker mentioned he's using OS X. – Aaron Miller Oct 09 '13 at 19:19
  • 5
    The fact that the question was about OS X is somewhat incidental. The question was, more generally, about getting terminal emacs' kill ring to play nicely with the clipboard and I'm fairly certain that other GNU/Linux users (besides myself) will find it in this context. So this is a useful answer, IMO. (It was certainly useful to me -- thanks elviejo!) – edam Feb 10 '14 at 15:22
  • @edam that was the point. Leave the information here so that other person googling for the answer could benefit. Glad it was useful to you. – elviejo79 Feb 10 '14 at 22:26
  • 6
    To make it work, I also had to add these two lines to my .emacs: (add-to-list 'load-path "~/.emacs.d/elpa/xclip-1.3/") and (require 'xclip). – tflutre Jul 26 '14 at 14:10
  • This does not work for me on Ubuntu 16.04 and Emacs 24. xclip is installed correctly and I can see xclip functions, etc., but when copying text it still does not appear in the OS clipboard for pasting into other applications. – ely Nov 10 '16 at 15:40
  • Notice: xclip plugin installed by el-get-install has no xclip-mode function. – Big Shield Mar 26 '17 at 13:12
  • 1
    Thank you! Being able to ggyG in emacs -nw is a blessing) – Sviatoslav Zalishchuk Jul 20 '18 at 08:39
  • I have done this but when I do paste operation it becomes very slow and it does not capture the copied text from another `emacs`. Please note that `xclock` works on the remote machine. @elviejo79 – alper Jul 20 '20 at 11:39
10

x-select-text is only used if you're running Emacs in a GUI. (Emacs maps the Mac/Windows pasteboard/clipboard APIs to the X11 model, hence the name). You can always use C-h f to find out more about a function like this one and view its definition if it's written in elisp.

On the Mac, there is no concept of CLIPBOARD versus PRIMARY selections, so there is no point in setting x-select-enable-clipboard.

The whole point of running emacs -nw is that it doesn't interact with the windowing system. Why use Emacs in a terminal when there are plenty of graphical Emacsen that work very nicely on the Mac?

That said, if you really wanted to hook up terminal Emacs to the Mac pasteboard, you could do something like this:

(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))))
Nicholas Riley
  • 40,933
  • 5
  • 98
  • 123
1

If you want a way to place the contents of the emacs region onto the clipboard only sometimes, as opposed to every time you do an emacs yank (which causes the clipboard contents the be overwitten all the time), you should check this answer to a related question:

https://stackoverflow.com/a/19625063/3363328

I found that it solved my problem much better than setting xclip mode.

Community
  • 1
  • 1
Silvio Levy
  • 167
  • 1
  • 8