I added Mac clipboard integration to my emacs -nw
using the following code, adapted from here:
(defun copy-from-osx ()
(shell-command-to-string "pbpaste"))
(defun paste-to-osx (text &optional push)
(let ((process-connection-type nil))
(let ((proc (start-process "pbcopy" "*Messages*" "pbcopy")))
(process-send-string proc text)
(process-send-eof proc))))
(setq interprogram-cut-function 'paste-to-osx)
(setq interprogram-paste-function 'copy-from-osx)
However, this creates some undesirable behavior in step 4 of the following situation:
- I kill the text
abc
. - I kill the text
def
. - I press
C-y
, which brings backdef
. - I press
M-y
, which should replacedef
withabc
but instead does nothing. - I press
M-y
again, which now properly replacesdef
withabc
. - Further invocations of
M-y
will function as expected, yanking kills from earlier on the kill ring.
(Having just the above code as my init.el
reproduces the problem.)
Here is what I think is happening:
When I kill some text, it's saved to the Emacs kill ring but also (via interprogram-cut-function
) to the system clipboard. Then, when I yank with C-y
, I first get the contents of the system clipboard (via interprogram-paste-function
). Pressing M-y
after that gets me the first entry in the Emacs kill ring, which is the same thing. Only by pressing M-y
a second time can I get to older entries in the kill ring.
I'm not sure what the best resolution to this problem is. Can Emacs be told to skip yanking from the system clipboard if its contents are identical to the first entry in the kill ring? Or is there another way to be able to C-y M-y
instead of C-y M-y M-y
to get the second-most-recent thing I killed?
--with-ns
comes with OSX clipboard integration out of the box -- i.e., no need to usepbcopy
. If this question relates only to using Emacs in the terminal, or some other unspecified usage, then please consider editing the question and/or title so that future forum and Google searchers are not confused.