13.3.2 Cut and Paste with Other Window Applications

Under the X Window System, PGTK and Haiku, there exists a primary selection containing the last stretch of text selected in an X application (usually by dragging the mouse). Typically, this text can be inserted into other X applications by mouse-2 clicks. The primary selection is separate from the clipboard. Its contents are more fragile; they are overwritten each time you select text with the mouse, whereas the clipboard is only overwritten by explicit cut or copy commands.

Under X, whenever the region is active (see The Mark and the Region), the text in the region is saved in the primary selection. This applies regardless of whether the region was made by dragging or clicking the mouse (see Mouse Commands for Editing), or by keyboard commands (e.g., by typing C-SPC and moving point; see Setting the Mark).

If you change the variable select-active-regions to only, Emacs saves only temporarily active regions to the primary selection, i.e., those made with the mouse or with shift selection (see Shift Selection). If you change select-active-regions to nil, Emacs avoids saving active regions to the primary selection entirely.

To insert the primary selection into an Emacs buffer, click mouse-2 (mouse-yank-primary) where you want to insert it. See Mouse Commands for Editing. You can also use the normal Emacs yank command (C-y) to insert this text if select-enable-primary is set (see Using the Clipboard).

By default, Emacs keeps the region active even after text is selected in another program; this is contrary to typical X behavior. To make Emacs deactivate the region after another program places data in the primary selection, enable the global minor mode lost-selection-mode.

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.