I use the brilliant answer from my previous question to copy selected regions of text from Emacs onto my system pasteboard (i.e. the OSX clipboard).

But some functions (e.g. gist-buffer) push output (e.g. a URL) onto the Emacs kill ring, and I'd like to be able to access that text from other OSX apps. (Without having to do yank, highlight region, then pbcopy like I currently do.)

So: How can I push text directly from the Emacs kill-ring to the OSX pasteboard?


The function x-select-text can be used to do this:

Select TEXT, a string, according to the window system.

On X, if `x-select-enable-clipboard' is non-nil, copy TEXT to the
clipboard.  If `x-select-enable-primary' is non-nil, put TEXT in
the primary selection.

So we need to setx-select-enable-clipboard to a non-nil value (e.g., t). On Linux machines, it's set to t by default, so you may or may not need to do this.

(setq x-select-enable-clipboard t)

(defun copy-current-kill-to-clipboard ()
  (x-select-text (current-kill 0)))

I don't have an OSX machine to test on -- only Linux --, but this should all work the same on OSX.

  • This won't work for me, since I have interprogram-cut-function and interprogram-paste-function set to nil. So I need to push the kill ring to the pasteboard manually. – incandescentman Jul 29 '15 at 16:22
  • I updated the answer to include code to do that. – zck Jul 29 '15 at 16:48
  • 3
    PSA: x-select-enable-clipboard var name is getting deprecated in emacs 25+ and is being replaced with select-enable-clipboard. Similarly x-select-enable-primary -> select-enable-primary. – Kaushal Modi Jul 31 '15 at 14:55
  • So what's a better way to code it? – incandescentman Jul 31 '15 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.