How to copy to clipboard in Emacs Lisp

I do this:

(with-temp-buffer
  (insert "Hello World")
  (clipboard-kill-region (point-min) (point-max)))

That gets it on the clipboard. If you want it on the kill-ring add a kill-region form also.


You're looking for kill-new.

kill-new is a compiled Lisp function in `simple.el'.

(kill-new string &optional replace yank-handler)

Make string the latest kill in the kill ring.
Set `kill-ring-yank-pointer' to point to it.
If `interprogram-cut-function' is non-nil, apply it to string.
Optional second argument replace non-nil means that string will replace
the front of the kill ring, rather than being added to the list.

Optional third arguments yank-handler controls how the string is later
inserted into a buffer; see `insert-for-yank' for details.
When a yank handler is specified, string must be non-empty (the yank
handler, if non-nil, is stored as a `yank-handler' text property on string).

When the yank handler has a non-nil PARAM element, the original string
argument is not used by `insert-for-yank'.  However, since Lisp code
may access and use elements from the kill ring directly, the string
argument should still be a "useful" string for such uses.

Tags:

Emacs

Elisp