10

Occasionally, without meaning to I somehow mash keys or mouse so that I get a secondary selection. I never use the secondary selection so I would always like to get rid of it.

How can I clear the secondary selection without using the mouse?

The default bindings for setting the secondary selection are intercepted by my window manager, so do not work in emacs. I don't really want to rebind these commands to other mouse events either: I use my mouse just for a few things, they work fine and I don't want to change them. I can't simply run commands like mouse-start-secondary: when I try that, emacs lets me know they have to be bound to an event.

Croad Langshan
  • 3,192
  • 14
  • 42

2 Answers2

11

You should be able to clear the secondary selection by evaluating:

(delete-overlay mouse-secondary-overlay)

If you never want to get the secondary selection I believe you can just unbind the default mouse events:

(global-unset-key [M-mouse-1])
(global-unset-key [M-drag-mouse-1])
(global-unset-key [M-down-mouse-1])
(global-unset-key [M-mouse-3])
(global-unset-key [M-mouse-2])
glucas
  • 20,175
  • 1
  • 51
  • 83
  • I get: Symbols's value as variable is void: secondary-mouse-overlay – Croad Langshan Feb 13 '15 at 22:10
  • Sorry - I got the name wrong. Fixing it now. – glucas Feb 15 '15 at 23:06
  • I'm unable to test this and mark it as accepted answer since I don't know how to get a secondary selection! When I mark something with secondary selection it's always by accident, so I have no idea how I do it. Gnome shell seems to intercept all mouse combinations I try... (though I'm not sure what M-down-mouse-1 means) – Croad Langshan Mar 08 '15 at 15:21
  • I can hold down Alt and the left mouse button to select text with the secondary selection, but no idea about the Gnome interaction. – glucas Mar 08 '15 at 15:36
  • Another way you might be getting the secondary selection is with Alt + right-mouse click. That will set the secondary selection from point to wherever you clicked. There are also bindings for Alt + double- or triple-clicking the various buttons. I suspect your window manager steals some but not all of those bindings. – glucas Mar 08 '15 at 15:45
  • Last comment: there is a comment here http://www.emacswiki.org/emacs/SecondarySelection that starting the mouse action and then hitting Alt might bypass the window manager. – glucas Mar 08 '15 at 15:50
  • I've tried all the suggestions in these comments and still can't get a secondary selection (except by accident I'm sure) :-( – Croad Langshan Mar 14 '15 at 20:17
4

You say that you never want to use the secondary selection. In that case, just bind the keys that are bound to secondary-selection commands by default to commands that you find more useful. Or unbind them, by binding them to nil. These are those keys:

  • M-mouse-1 (mouse-start-secondary, by default)
  • M-drag-mouse-1 (mouse-set-secondary, by default)
  • M-down-mouse-1 (mouse-drag-secondary, by default)
  • M-mouse-2 (mouse-yank-secondary, by default)
  • M-mouse-3 (mouse-secondary-save-then-kill, by default)

For example:

(global-set-key [M-mouse-1] 'a-handy-mouse-command)

On the other hand, if the problem is that your window manager steals those keys, then consider binding different keys to those commands. For instance, add modifier Control to each of the default keys: C-M-mouse-1 etc.

(global-set-key [C-M-mouse-1] 'mouse-start-secondary)

And if the problem is that you don't want to use the mouse for manipulating the secondary selection, then you can use the keyboard instead. Library second-sel.el can help with this and other ways to use the secondary selection.

For example, you can get rid of the secondary selection interactively anytime this way, if you bind secondary-dwim to C-M-y (for example):

  1. C-SPC - to create an empty active region.
  2. C-1 C-M-y - to move the secondary selection to the region (which is empty).

This is the doc string of command secondary-dwim, which second-sel.el binds by default to C-M-y:

C-M-y runs the command secondary-dwim, which is an interactive
compiled Lisp function in `second-sel.el'.

It is bound to C-M-y, menu-bar edit secondary-dwim.

(secondary-dwim ARG)

Do-What-I-Mean with the secondary selection.
Prefix arg:

 None: Yank secondary.
 Zero: Select secondary as region.
 > 0:  Move secondary to region.
 < 0:  Swap region and secondary.

Details:

No prefix arg: Yank the secondary selection at point.  Move point to
the end of the inserted text.  Leave mark where it was.

Zero arg: Select the secondary selection and pop to its buffer.

Non-zero arg: Move the secondary selection to this buffer's region.

Negative arg: Also go to where the secondary selection was and select
it as the region.  That is, swap the region and the secondary
selection.
Drew
  • 75,699
  • 9
  • 109
  • 225