9

I noticed that when I copy thing outside of Emacs, the thing does not appear to kill ring immediately except I yank it to Emacs. If I copy twice, only the last copy can be got by yank.

Sometimes, I would like to push the copys from system clipboards to kill ring once I do the copy. After that, I can select what I need in the kill ring to paste in Emacs. So How can I achieve that the copys can be pushed into kill ring many times?

I am on Windows 7.

Leu_Grady
  • 2,420
  • 1
  • 17
  • 27

3 Answers3

5

TL;DR: you can use clipmon available on MELPA, and turn on clipmon-mode.

Note: the details below apply to X servers, but the package should work on any platform.

How does the clipboard work ? Clipboard is implemented as an asynchronous process. When you copy from an application, it becomes the "clipboard owner" but doesn't actually put the copied data anywhere. When you paste into an application, it requests the data from the current "clipboard owner".

Reference: http://www.jwz.org/doc/x-cut-and-paste.html

How to get the content of the clipboard As I understand it, the "reliable" way to see if there's new data in the clipboard is to periodically poll the X server to check what's there. For emacs, the package clipmon (https://github.com/bburns/clipmon) does that. Every two seconds by default.

Excerpt from the README :

It also adds changes to the system clipboard to the kill ring, making Emacs into a clipboard manager for text - you can then use a package like browse-kill-ring or helm-ring to view and manage your clipboard history.

(Please note that I didn't actually try this package, but it certainly looks like a solution.)

YoungFrog
  • 3,496
  • 15
  • 27
3

I'm not sure that it is possible to have copied text appear in your kill-ring immediately after copying, unless there is some way to run a hook on copy. This would obviously depend on your operating system/environment, but I did want to mention that you might want to try setting

(setq save-interprogram-paste-before-kill t)

Which will at least preserve copied text from the operating system when something is copied from within emacs.


From C-h v save-interprogram-paste-before-kill:

Save clipboard strings into kill ring before replacing them.

When one selects something in another program to paste it into Emacs, but kills something in Emacs before actually pasting it, this selection is gone unless this variable is non-nil, in which case the other program's selection is saved in the kill-ring before the Emacs kill and one can still paste it using C-y M-y.

Kaushal Modi
  • 25,203
  • 3
  • 74
  • 179
imalison
  • 211
  • 1
  • 5
  • Yes, this variable can do exactly what you said. But I am not just asking for that, I want the kill ring can save copy outside of Emacs once I copied. – Leu_Grady Aug 04 '15 at 11:07
2

Finally I figured out a clumsy solution, which referred to the concept of clipmon. But very simple with one line's code:

(run-at-time 0 3 (lambda () (interactive) (current-kill 0)))

The kill ring will be synchronized every 3 seconds with system clipboard. Of course you can alter the interval to a even smaller value.

PS: The reason why I need this functionality is that I want to define a command that can be invoked with arguments of what I copied exteriorly.

Thanks for the responses from @kaushalmodi and @YoungFrog.

Leu_Grady
  • 2,420
  • 1
  • 17
  • 27