Skip to content

Instantly share code, notes, and snippets.

Embed
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))
@speedarius
Copy link

speedarius commented Feb 9, 2012

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

@the-kenny
Copy link
Author

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.

@speedarius
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.

@suvash
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 !

@mdesjardins
Copy link

mdesjardins commented Oct 21, 2013

ZOMG Thank you so much for this!

@scudelletti
Copy link

scudelletti commented Aug 5, 2014

Thanks a lot! 👍

@tonini
Copy link

tonini commented Nov 11, 2014

This is just great! :D

typo:

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

There is one ) too much.

@galderz
Copy link

galderz commented Apr 27, 2015

@jslu0418
Copy link

jslu0418 commented May 15, 2016

It works like a charm, thanks!

@subhashb
Copy link

subhashb commented Jun 5, 2016

Worked right off the bat! 👍

@gudmunduregill
Copy link

gudmunduregill commented Sep 7, 2016

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

@fantimond
Copy link

fantimond commented Apr 8, 2019

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

@breakWindy
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