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. IOW, think of the secondary selection as something less ephemeral than the region, something that is unaffected by changes in the region.
How do you use the secondary selection? See the Emacs manual, node Secondary Selection (duh…). 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:
(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
‘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
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-dwim’ rolls several secondary-selection commands into one. Library setup-keys.el binds this to
Its behavior depends on the prefix argument:
‘yank-secondary’(also defined in
second-sel.el): Yank the secondary selection at point.
‘secondary-to-primary’: Select the secondary selection as the region, popping to its buffer.
‘secondary-swap-region’: Swap the region and the secondary selection. That is, also go to where the secondary selection was and select it as the region.
DWIM summary: Use
‘C-- C-M-y’), so you can easily bounce back.
Library setup-keys.el binds command
‘M-y’. It cycles among selections in a selection ring: either the
‘kill-ring’ or the secondary-selection ring.
If the previous command was not a yank at all (from either the
‘kill-ring’ or the
‘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.
‘M-y’means browse the last-used selection ring. With a prefix argument, or if you are already in a selection-ring browse buffer, it browses the other selection ring.
‘M-y’means yank by completing against either the
‘kill-ring’or (with a prefix argument) the
‘M-y’yanks a kill using completion, and
‘C-u M-y’yanks a secondary selection using completion.
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.
‘set-secondary-start’(I bind it to
‘C-x C-M-SPC’) sets the start of the secondary selection at point. This also undefines it, e.g., if it was defined in some other buffer.
‘secondary-save-then-kill’(I bind it to
‘C-x C-M-RET’) does the same thing that
‘mouse-secondary-save-then-kill’does (e.g., bound to
‘M-mouse-3’), but it uses point instead of the
‘mouse-3’click position. That is, it sets, extends/retracts, or deletes the secondary selection.