40

I want to copy a string to the clipboard (not a region of any particular buffer, just a plain string). It would be nice if it were also added to the kill-ring. Here's an example:

(copy-to-clipboard "Hello World")

Does this function exist? If so, what is it called and how did you find it? Is there also a paste-from-clipboard function?

I can't seem to find this stuff in the Lisp Reference Manual, so please tell me how you found it.

1

4 Answers 4

66

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.
3
  • 1
    Looks very promising. How did you find this?
    – User1
    Feb 1, 2010 at 18:23
  • I needed it myself many, many years ago. Can't remember how I found it. Feb 1, 2010 at 18:24
  • 3
    Actually, the description says it works with the kill-ring, which is an internal structure that may or may not be related to clipboard. For me in particular, (kill-new "hello") doesn't modify clipboard.
    – Hi-Angel
    Mar 14, 2019 at 23:23
5

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.

1
  • copy-region-as-kill can copy to clipboard and kill-ring in one go: copy-region-as-kill is an interactive compiled Lisp function in simple.el'. (copy-region-as-kill beg end) Save the region as if killed, but don't kill it. In Transient Mark mode, deactivate the mark. If interprogram-cut-function' is non-nil, also save the text for a window system cut and paste. Feb 1, 2010 at 18:26
0

The command to put your selection on the window system clipboard is x-select-text. You can give it a block of text to remember. So a (buffer-substring (point) (mark)) or something should give you what you need to pass to it. In Joe's answer, you can see the interprogram-cut-function. Look that up for how to find this.

0

In my .emacs file, i use this

(global-set-key "\C-V" 'yank)
(global-set-key "\C-cc" 'kill-ring-save)

I could not use Ctrl-C (or System-copy), but this may be enough in case old habits kick in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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