When running Emacs under the X window system, you can easily transfer text between Emacs and other X applications using the primary selection (also called the X selection). This is not the same thing as the clipboard, which is a separate facility used on desktop environments such as Gnome, and on operating systems such as Microsoft Windows (see Clipboard).
Under X, whenever you select some text in Emacs by dragging or clicking the mouse (see Mouse Commands), it is also saved in the primary selection. You can then paste that text into any other X application, usually by clicking Mouse-2 in that application. Unlike the Emacs kill ring (see Kill Ring), the primary selection has no “memory”: each time you save something in the primary selection, either in Emacs or in another X application, the previous contents of the primary selection are lost.
MS-Windows provides no primary selection, but Emacs emulates it within a single Emacs session, by storing the selected text internally. Therefore, all the features and commands related to the primary selection work on Windows as they do on X, for cutting and pasting within the same session, but not across Emacs sessions or with other applications.
Whenever you kill some text using a command such as C-w
kill-region), or copy it into the kill ring using a command
such as M-w (
kill-ring-save), that text is also saved in
the primary selection. See Killing.
If you set the region using the keyboard—for instance, by typing
C-<SPC> and moving point away from the mark—the text in
the region is not normally saved to the primary selection. However,
if you change the variable
the region is saved to the primary selection whenever you activate the
mark. Each change to the region also updates the primary selection.
If you change
the kill ring with M-y (
yank-pop) also saves the new yank
to the primary selection (see Yanking).
If you change
t, each kill command first saves the existing selection onto
the kill ring. This prevents you from losing the existing selection,
at the risk of large memory consumption if other applications generate
Whenever Emacs saves some text to the primary selection, it may also
save it to the cut buffer. The cut buffer is an obsolete
predecessor to the primary selection; most modern applications do not
use it. Saving text to the cut buffer is slow and inefficient, so
Emacs only does it if the text is shorter than the value of
x-cut-buffer-max (20000 characters by default).
You can yank the primary selection into Emacs using the usual yank
commands, such as C-y (
yank) and Mouse-2
mouse-yank-at-click). These commands actually check the
primary selection before referring to the kill ring; if no primary
selection is available, the kill ring contents are used. To prevent
yank commands from accessing the primary selection, set the variable
The standard coding system for the primary selection is
compound-text-with-extensions. You may find that the pasted
text is not what you expected. In such a case, you can specify
another coding system for the selection by typing C-x <RET>
x or C-x <RET> X. Alternatively, you can request a
different data type by modifying the variable
x-select-request-type. See Communication Coding.