Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
pbcopy & pbpaste for Emacs
(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))
Copy link

speedarius commented Feb 9, 2012

Hey, I found this from a Google search, was exactly what I needed. Thanks!

Copy link

the-kenny commented Feb 9, 2012

Nice to hear! I'll check these days if Emacs 24 provides a better way of doing this.

One problem with this piece of code is encoding. It sometimes messes up unicode and I'm too lazy to fix this. Please let me hear if you find something better.

Copy link

speedarius commented Nov 10, 2012

So I decided to try out tmux. It's great, except that I couldn't get it to work with Emacs. For some reason, C-y was not yanking like it is supposed to. After much debugging, I finally traced it back to this code that I had copied into my init.el. If I comment this out, C-y works in Emacs under tmux. I don't know exactly what the problem is; perhaps something related to the encoding issue you mentioned. Anyway, just an FYI for the next guy.

Copy link

suvash commented Aug 14, 2013

Thanks a lot for this. I've been using Emacs 24.3.1 and it works flawlessly (so far). Also works inside a tmux window/pane. Cheers !

Copy link

mdesjardins commented Oct 21, 2013

ZOMG Thank you so much for this!

Copy link

scudelletti commented Aug 5, 2014

Thanks a lot! 👍

Copy link

tonini commented Nov 11, 2014

This is just great! :D


(setq interprogram-paste-function 'copy-from-osx))

There is one ) too much.

Copy link

galderz commented Apr 27, 2015

Copy link

jslu0418 commented May 15, 2016

It works like a charm, thanks!

Copy link

subhashb commented Jun 5, 2016

Worked right off the bat! 👍

Copy link

gudmunduregill commented Sep 7, 2016

Found this from Google search. Exactly what I wanted. Very nice indeed.

Copy link

fantimond commented Apr 8, 2019

@the-kenny add (setenv "LANG" "en_US.UTF-8") can solve the non-ascii mess-up problem.

Copy link

breakWindy commented Dec 2, 2021

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment