How do I disable pasting when clicking the middle mouse button/wheel? [duplicate]

Is there a way to disable the middle mouse button paste behavior that is here by default on gnome?

I have a sensitive mouse wheel and whenever I scroll texts, sometimes it pastes stuff randomly into the text. I lose quite a lot of credibility when I send a file to someone else that has random text snippets pasted all over it.

I have seen a solution that goes by mapping the mouse's middle button to a non-existant mouse button, but that implies getting rid of the middle mouse button altogether (i.e. no tab-closing, opening links into a new tab automatically, etc.). I'd like to keep my middle mouse button active, just disable the pasting behavior.

This also happens when I scroll text with my touchpad (accidentally hit two-fingers without moving, bam.)

So the problem will not be fixed just by changing for a new mouse (in fact I believe it happens more often with my touchpad than with my mouse).


Solution 1:

I use gnome-tweak-tool for disabling middle button paste in Ubuntu 16.04.

  1. Install it

    sudo apt install gnome-tweak-tool
    
  2. Run it by searching "tweak tool" in installed apps or just type gnome-tweak-tool in a terminal.

  3. Go to "Keyboard and mouse" -> "Middle-click paste"
  4. Turn off.

    screenshot

That's it.

Or using just CLI

gsettings set org.gnome.desktop.interface gtk-enable-primary-paste false

Tested on 16.04.

Solution 2:

I realise that this is not exactly the answer you want, but you can turn this off in Firefox (e.g. if you don't mind the feature elsewhere, but still want middle click in Firefox to open links in new tabs)

In about:config, set

middlemouse.contentLoadURL false
middlemouse.paste false

Not what you asked, but as this question is linked to from a few places I hope someone finds this answer useful.

Solution 3:

Jared Robinson gave a simple solution that works on my machine:

Run the following command:

xmodmap -e "pointer = 1 25 3 4 5 6 7 8 9"

To persist this behavior, edit ~/.Xmodmap and add

pointer = 1 25 3 4 5 6 7 8 9

Solution 4:

This currently isn't possible - though, as you have mentioned, there are ways to disable the MOUSE 3 button - or remap it- none of those get at the source of the issue. The X11 Primary Selection.

While this isn't a solution, hopefully this explanation will make it clear WHY. In Ubuntu there are two clipboards at work. One, which everyone is familiar with, the freedesktop.org clipboard (captures Ctrl+C command) The second is a clipboard manager that has been at play since before Ubuntu even existed - X11. The X Server (X11) manages three other clipboards: Primary Selection, Secondary Selection, and Clipboard. When you select text with your pointer it gets copied to a buffer in the XServer, the Primary Selection, and awaits pasting by means of the Mouse 3 button. The other two were designed to be used by other applications in a means to share a common clipboard between applications. In this case the freedesktop.org clipboard manager in Ubuntu already does this for us.

Through the extent of my research I can not find a way to disable the X11 selection manager. There are no compilation flags, applications, or configuration values that can disable this. There are various ways around this on a per application basis (majority of these applications being command line ones) - but nothing on a global scale.

I realize this isn't an ideal solution - but seems to be the truth to the issue. The only relevant solution I could muster is actually a hack, create a script that executes an infinite while loop that just replaces the Primary Selection with a null value.

First install xsel (Tool for manipulation of the X selection) sudo apt-get install xsel

The code is as follows:

while(true)
do
    echo -n | xsel -n -i
    sleep 0.5
done

If you place this in a script and add it to your startup scripts this shouldn't be an issue.

Solution 5:

Somehow, I ended up without any xmodmap files on my Ubuntu install, so I had to find a different approach to this problem.

Take a look at the xinput command.

xinput list | grep -i mouse

which lists information about your mouse. It shows my mouse is "Dell Premium USB Optical Mouse" and also that I have "Macintosh mouse button emulation". Armed with that info, I can

xinput get-button-map "Dell Premium USB Optical Mouse"

which gives me a listing that looks like

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

Here is the useful, required knowledge. My mouse has, theoretically, 18 buttons. Each button's default action has the same name as it's button number. In other words, button 1 does action 1, button 4 does action 4, etc. Action 0 means "off".

The position in the listing shows the function assigned to that button. So if my button map read

1 3 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

this would mean button 1 (position 1) does action 1 (normal left button), button 2 (position 2) does action 3 (middle button) and button 3 (position 3) does action 2 (right button).

To make a left handed mouse all you would need would be a button map that starts

3 2 1 4 5 .....

Or, in your case, it looks like you want the middle button to do the same thing as button 1 (left button) so your map needs to start

1 1 3 ....

I'd reset my mouse button mappings thus:

xinput set-button-map "Dell Premium USB Optical Mouse" 1 1 3 5 6 6 7 8 9 10 11 12 13 14 15 16 17 18

In your case, you may have a different number of mapped buttons and have some special button map already defined. Likwely, your mouse has a different name, too. First, get your mouse's "name". Then, use the get-button-map operation to find your base button map. finally, use the set-button-map option, modifying button 2 to do action 1.

This is not a permanent change. I added the necessary code to my .bashrc so it executes every time I login or open a terminal.

Hope this helps.