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?


2 Answers 2


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. Jul 29, 2015 at 16:22
  • I updated the answer to include code to do that.
    – zck
    Jul 29, 2015 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. Jul 31, 2015 at 14:55
  • So what's a better way to code it? Jul 31, 2015 at 17:06

I think gui-set-selection is your friend:

— Command: gui-set-selection type data This function sets a window-system selection. It takes two arguments: a selection type type, and the value to assign to it, data.

type should be a symbol; it is usually one of PRIMARY, SECONDARY or CLIPBOARD. These are symbols with upper-case names, in accord with X Window System conventions. If type is nil, that stands for PRIMARY.

If data is nil, it means to clear out the selection. Otherwise, data may be a string, a symbol, an integer (or a cons of two integers or list of two integers), an overlay, or a cons of two markers pointing to the same buffer. An overlay or a pair of markers stands for text in the overlay or between the markers. The argument data may also be a vector of valid non-vector selection values.

This function returns data.


(defun copy-current-kill-to-clipboard ()
(gui-set-selection 'CLIPBOARD (current-kill 0)))

should do the job without any prerequisites (e.g. x-select-enable-clipboard being t).

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.