4

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?

incandescentman
  • 4,111
  • 16
  • 53
  • Hmm, I always thought that newer versions of Emacs put contents of kill ring into clipboard by default, at least this is how it works for me. I'm using GNU/Linux, though. – Mark Karpov Jul 29 '15 at 15:53
  • Maybe they do, but I guess I'm not using default settings. I'd Iike to do this manually. – incandescentman Jul 29 '15 at 15:54
  • 1
    This may be of some interest for you: https://www.gnu.org/software/emacs/manual/html_node/emacs/Clipboard.html – Mark Karpov Jul 29 '15 at 15:57

2 Answers2

4

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 ()
  (interactive)
  (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.

Kaushal Modi
  • 25,203
  • 3
  • 74
  • 179
zck
  • 8,984
  • 2
  • 31
  • 65
1

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.

So

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

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