SecondarySelection

Emacs has a primary selection, called the region. It also has a secondary selection. What for?

Use the secondary selection for, well, a second selection. This lets you use the region for other things, while keeping another selection available for yanking etc. In other words, think of the secondary selection as something less ephemeral than the region, something that is unaffected by changes in the region, i.e., a selection that stays put even as you move the cursor to a different position (point).

Using the Secondary Selection (Vanilla Emacs)

How do you use the secondary selection? See Secondary Selection. Selection and yanking are the same as for the region, except that you use the Meta modifier key.

You can also use the secondary selection as another way to do what you do with ‘query-replace’ – sometimes it is more convenient or more flexible:

  1. Select the replacement text as the secondary selection.
  2. Select the text to replace as the primary selection.
  3. Paste the secondary selection.
  4. Kill the primary selection.
the above doesn’t works on Linux (Ubuntu) because most meta mouse stuff is intercepted by X (Meta drag moves the window, for example) Is there a way to use secondary selection with Windows key instead of Meta key? – Anonymous (or should I say Anony Mouse?)
Initiating the mouse action before hitting meta seems to bypass the window manager and get to emacs. – Anonymous
I bind the moving of the window to Super+Mouse1 (Super being indeed the windows-logo key); I replace many Alt+Something bindings for the desktop to Super+Something, preferring to use the Alt key for local (application specific) bindings. – ShinTakezou
I had problems copy-pasting between emacs and other applications that expect the data is in the clipboard, so I’ve added (setq x-select-enable-clipboard t) to my .emacs file; I don’t know if this is good or not, but it worked for me. – ShinTakezou
FWIW, I too use ‘t’ for ‘x-select-enable-clipboard’. I also use ‘C-M-’, not just ‘M-’, for all of my secondary-selection bindings. (But I am on Windows mostly now, not GNU/Linux.) – DrewAdams
Is the secondary selection in emacs speak the same thing as the secondary selection in X Windows speak? – Anonymous
I think so, to the extent that Emacs can faithfully reflect the latter. But someone might correct me. – DrewAdams

Library `second-sel.el' - Secondary Selection Enhancements

Library second-sel.el enhances secondary selection.

It adds the secondary selection to a new selection ring, ‘secondary-selection-ring’, which is similar to, but separate from, the ‘kill-ring’. It also prevents the secondary selection from being added to the ‘kill-ring’, so you now have two separate selection-history rings.

‘M-y’ cycles items from the appropriate ring, depending on whether it follows a yank of the primary (region) or the secondary selection (see below).

second-sel.el defines new secondary-selection commands. Library menu-bar+.el adds some of these to the Edit menu as items Paste Secondary, Move Secondary to Region, Swap Region and Secondary, and Select Secondary as Region.

[:secondary-yank|select|move|swap]

`secondary-yank|select|move|swap' (`C-M-y')

Command `secondary-yank|select|move|swap’ rolls several secondary-selection commands into one. Library setup-keys.el binds this to ‘C-M-y’.

Its behavior depends on the prefix argument:

DWIM summary: Use ‘C-M-y’:

`isearch-yank-secondary' (`C-M-y' in Isearch)

In Isearch, ‘M-y’ yanks the current kill into the Isearch string. Library setup-keys.el binds command ‘isearch-yank-secondary’ to ‘C-M-y’ for Isearch, so you can yank the current secondary selection.

`yank-pop-commands' (`M-y')

Library setup-keys.el binds command ‘yank-pop-commands’ to ‘M-y’. It cycles among selections in a selection ring: either the ‘kill-ring’ or the secondary-selection ring.

`M-y' at Top Level

If the previous command was not a yank at all (from either the ‘kill-ring’ or the ‘secondary-selection-ring’), then ‘M-y’ does nothing in vanilla Emacs. Here are two alternative enhancements that work together with second-sel.el to let you yank arbitrary entries from either selection ring.

Defining and Deleting the Secondary Selection using the Keyboard

The second-sel.el sections above show that you can use the keyboard instead of the mouse to yank the secondary selection. You can also use keyboard keys to define it, extend/retract it, and delete its text. Anything you can do with it using the mouse you can also do using keyboard keys.

Using a Touchpad with the Secondary Selection

If you’re using a touchpad (this is on CentOS 7.2), some actions are easy to remember:

If something happens which you don’t want, you can do C-/ to undo– multiple times, if desired.

See Also


CategoryEditing CategoryGlossary CategoryRegion