Oxonon
Oxonon

Reputation: 281

Change default EMACS mouse highlight behaviour

In EMACS the default seems to be to 'copy' any text highlighted with the mouse. I'm attempting to replicate the functionality of modern text editors, where I can highlight a section of text and press 'paste' to replace it. I have so far added

(delete-selection-mode 1)

to my init.el

The problem is, if I copy something, then highlight to paste in its place, I end up pasting what I had just highlighted, changing nothing.

What do I need to change to fix that behaviour?

Upvotes: 1

Views: 1022

Answers (3)

Jules
Jules

Reputation: 973

The most powerful element of emacs is its introspection features, lets have a look at how we can use them to try and solve this problem. We must use the power of the source.

One of the most important tools for introspection in emacs is the describe-key command which is bound to C-h k. It brings up the documentation of whatever keystroke is called after it. So in our case if we press C-h k and then click and drag we will see the documentation for <down-mouse-1> and more importantly for <drag-mouse-1>. The documentation states that "<drag-mouse-1> at that spot runs the command mouse-set-region". Below it then gives some documentation for this command. It says

Set the region to the text dragged over, and copy to kill ring. This should be bound to a mouse drag event. See the ‘mouse-drag-copy-region’ variable to control whether this command alters the kill ring or not.

Now we know that somehow mouse-drag-copy-region controls whether or not the highlighted text is copied.

If we follow the link of that variable it tells us the default value and some documentation:

If non-nil, copy to kill-ring upon mouse adjustments of the region.

Now all we have to do is set the variable to be nil to get the effect that you want. Place the following code at the end of your init file and you should be all set

(setq mouse-drag-copy-region nil)

I hope that this helps you with this problem and that more importantly it helps you with further issues.

Upvotes: 3

Lindydancer
Lindydancer

Reputation: 26094

By default, selecting a region with the mouse does not copy the text to the kill ring. If your Emacs does this, you probably have set the variable mouse-drag-copy-region.

In a fresh Emacs (24.5 started using -Q), you can do the following:

  • Start delete-selection-mode.
  • Mark a region using the mouse. Copy it using M-w.
  • Mark a second region. Replace it with the first using C-y.

Upvotes: 1

Drew
Drew

Reputation: 30701

I see two alternatives, neither of which does exactly what you request. (For both, yes, turn on delete-selection-mode.)

  • Use the secondary selection for the text to copy, and use the primary selection (the region) for the text to be replaced.

    You copy text into the secondary selection using the Meta key plus the mouse - for example, press and hold Meta (the Alt key, usually) while dragging or double-clicking mouse-1.

    You paste the secondary selection using Meta plus mouse-2.

  • Select text with the mouse, then copy it to the kill-ring using M-w. Then select the text to replace with the mouse and use C-y to paste the copied text to replace it.

Upvotes: 0

Related Questions