13.3.1 Using the Clipboard

The clipboard is the facility that most graphical applications use for “cutting and pasting”. When the clipboard exists, the kill and yank commands in Emacs make use of it.

When you kill some text with a command such as C-w (kill-region), or copy it to the kill ring with a command such as M-w (kill-ring-save), that text is also put in the clipboard.

When an Emacs kill command puts text in the clipboard, the existing clipboard contents are normally lost. Optionally, Emacs can save the existing clipboard contents to the kill ring, preventing you from losing the old clipboard data. If save-interprogram-paste-before-kill has been set to a number, then the data is copied over if it’s smaller (in characters) than this number. If this variable is any other non-nil value, the data is always copied over—at the risk of high memory consumption if that data turns out to be large.

Yank commands, such as C-y (yank), also use the clipboard. If another application “owns” the clipboard—i.e., if you cut or copied text there more recently than your last kill command in Emacs—then Emacs yanks from the clipboard instead of the kill ring.

Normally, rotating the kill ring with M-y (yank-pop) does not alter the clipboard. However, if you change yank-pop-change-selection to t, then M-y saves the new yank to the clipboard.

To prevent kill and yank commands from accessing the clipboard, change the variable select-enable-clipboard to nil.

Programs can put other things than plain text on the clipboard. For instance, a web browser will usually let you choose “Copy Image” on images, and this image will be put on the clipboard. On capable platforms, Emacs can yank these objects with the yank-media command—but only in modes that have support for it (see Yanking Media in The Emacs Lisp Reference Manual).

Many X desktop environments support a feature called the clipboard manager. If you exit Emacs while it is the current “owner” of the clipboard data, and there is a clipboard manager running, Emacs transfers the clipboard data to the clipboard manager so that it is not lost. In some circumstances, this may cause a delay when exiting Emacs; if you wish to prevent Emacs from transferring data to the clipboard manager, change the variable x-select-enable-clipboard-manager to nil.

Since strings containing NUL bytes are usually truncated when passed through the clipboard, Emacs replaces such characters with “\0” before transferring them to the system’s clipboard.

Prior to Emacs 24, the kill and yank commands used the primary selection (see Cut and Paste with Other Window Applications), not the clipboard. If you prefer this behavior, change select-enable-clipboard to nil, select-enable-primary to t, and mouse-drag-copy-region to t. In this case, you can use the following commands to act explicitly on the clipboard: clipboard-kill-region kills the region and saves it to the clipboard; clipboard-kill-ring-save copies the region to the kill ring and saves it to the clipboard; and clipboard-yank yanks the contents of the clipboard at point.