234

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).

Flyk
  • 1,480
levesque
  • 4,422
  • 35
    Really annoying default behaviour. How did you disable it? – umpirsky Jun 20 '11 at 14:09
  • 2
    @umpirsky: Haven't found a proper way to disable it yet. – levesque Jun 20 '11 at 16:04
  • 8
    If your mouse records middle clicks while you just want to scroll, I'd suggest you to try another mouse model (probably one with a higher pressure to click the wheel). After several years of Linux use I've never been bothered with your issue, actually the middle click paste proved itself to be quite useful :) – Maxime R. Jan 12 '12 at 14:50
  • There is nothing wrong with the mouse, the force to click the wheel is normal. I think it is the force and velocity at which I can scroll sometimes. Hence why I get pastes without noticing in random places in my files. I also don't think I will use the middle click to paste as I am used to my current method and switch OSes all the time anyway.... – neildeadman Jan 12 '12 at 14:52
  • 16
    I've got the same problem, and personally consider it a giant security flaw. Copying passwords and keys is not uncommon, and accidentally pasting them into random websites while attempting to scroll, or muscle memory is a nightmare come true. – Ryan Leach Nov 03 '18 at 16:23
  • 6
    Oh so I am not alone having this issue. First I was scared of my random texts pasted on some inputs on websites, only after while realized it's caused by system and mapped paste action to middle click - and ofc. it accidentally happens on my mouse when scrolling. Hope this will be removed as default in future. – Jurosh Dec 30 '18 at 15:43
  • gnome-tweak-tools - btw disabling middle-click feels like disabling keyboard shortcuts, buy a better mouse. – Francois Nov 01 '19 at 11:20
  • I hope I’ll find a proper solution to this! I keep pasting random stuff into Google docs that I’m reviewing, must to the confusion of the authors and other reviewers. It’s only a matter of time before I paste a password or an embarrassing link… On ThinkPads you scroll by keeping the middle button pressed and moving the cursor, so the slightest wiggling on the middle button pastes. – Dawn Drescher Sep 14 '21 at 08:04
  • This is not only annoying, but also a security risk. So many times I tried to open a link with the mouse wheel just to have some text pasted into a shared gdoc. Or the IDE. And then it actually got committed to git. Absolutely terrible feature, and I still haven't found a way to get rid of it across the board. – obe Jun 03 '22 at 14:34

28 Answers28

101

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.

Sunny127
  • 1,245
  • 1
    Worked like a charm! I was losing hope so quickly. Thank you! – theY4Kman Feb 08 '17 at 19:43
  • 11
    it doesn't work after restart :'( – Ahmad Muzakki Apr 02 '17 at 05:55
  • 1
    Worked, of course! Why isn't this the top answer? – CodeMouse92 Apr 19 '17 at 01:50
  • Link seems to be broken, can someone fix it? – Alexander Mills Jul 13 '18 at 20:02
  • 52
    Using the Tweaks tool would be by far the nicest way to do this, if it worked. I switched off the middle-click paste and it is still middle-click pasting same as before. Same after a restart. Anyone solve a similar issue? – Kvothe Jul 25 '18 at 22:53
  • 23
    @Kvothe: I just noticed that the setting in Tweak tools seems to affect only certain standard Gnome programs, like gedit, gnome-terminal, gnome-calculator etc. See also https://tinyurl.com/y7qtak7g (other question here, with no answer). I also noticed that the middle-button paste will copy whatever text was last marked, not even only what was explicitly copied. The clipboard will then be filled with that text. A really annoying feature. (When text was last marked in a standard Gnome program like gedit etc., however, the clipboard will just be made empty on middle-click.) – trollkotze Jul 30 '18 at 14:52
  • 8
    Also interesting, an explanation for the weird middle-click behaviour of overwriting the clipboard buffer with the current selection: https://askubuntu.com/a/225879/653860

    (But sadly, disabling the middle mouse button altogether, as proposed in that answer, also disables my mouse wheel completely. It seems there is no simple way to get rid of this stupid behaviour without destroying other crucial input functionality. Very bad design... :/ )

    – trollkotze Jul 30 '18 at 14:56
  • 3
    "The clipboard will then be filled with that text. A really annoying feature." Okay, no, I have to correct myself. That's not true. It seems the copy and paste clipboard is independent of the weird middle-click clipboard. The middle-click will always paste what is currently selected. The clipboard stays unaffected by that. – trollkotze Jul 30 '18 at 15:07
  • Mine doesn work either, but Iḿ using an (thinkpad) external keyboard (Iḿ using the middle/scroll button on the keyboard. In case it makes any difference... I can install Tweaks and deactivate pasting in middle button, but the option refers to a mouse, which I don use, and they keyboard midle scrolling button keeps pasting (probably less frequently though) Any ideas about what to do in this case? – Martin Oct 27 '18 at 12:08
  • This is by far the best answer to this question! – nkoren Jan 09 '19 at 17:48
  • does this work with DE other than gnome (kde, xfce, ...) ? – Yukulélé Feb 25 '19 at 08:58
  • I had to additionally disable mouse click emulation to make it work so it doesn't happen with my touchpad on Ubuntu 19.04. – mudassirkhan19 Oct 17 '19 at 07:54
  • 3
    Using Ubuntu 18.04, disabling this option in the tweak tool still results in the middle click acting as paste... – Aaron Franke Feb 02 '20 at 04:50
  • 1
    The CLI command works for me on Ubuntu 19.10 without further steps. – Max Murphy Apr 05 '20 at 00:39
  • 1
    This doesn't seem to work in Ubuntu Mate 18.04; poking around in dconf-editor, there is a key in org.gnome.desktop.interface but it doesn't seem to have any effect. I tried looking for a Mate specific one, too, there doesn't seem to be an obvious setting in org.mate.desktop or org.mate.desktop.interface and there's no way to "create" a key there in case it was default... hidden or something. – jrh Apr 06 '20 at 15:45
  • It is not working for me either. :( I haven't tried restarting yet. – Caleb Stanford Apr 10 '20 at 15:52
  • 7
    No longer works on 20.04 – Sam Johnson Feb 22 '21 at 16:09
  • 1
    Working partially on 22.04. Now the middle click does not work anymore in the terminal (okay) but still works on other apps like LyX. Annoying. – user171780 Jan 31 '23 at 11:44
  • 2
    this package has been renamed to gnome-tweaks – Hassan Abdel-Rahman Nov 08 '23 at 14:44
75

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.

Ben
  • 759
48

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
Tapper
  • 2,730
  • 48
    Doesn't this just disable the middle button altogether? What about closing tabs, opening links to new tabs, etc.? – levesque Aug 07 '11 at 17:36
  • 1
    Thanks, this fixed it. Now I get context menu when clicking wheel, which is much better then paste. – umpirsky Nov 30 '11 at 07:59
  • +1 This should be marked as the answer to this question. – Dave Feb 07 '14 at 19:52
  • 36
    @HDave How on earth should this be marked as the answer to a question that specifically states he's seen solutions that rebind the middle mouse to a different key, but those are not satisfactory? That's exactly what this answer does. – Vala Apr 02 '14 at 14:56
  • This is a great answer. Is there a way to retrieve the previous pointer settings, save them in a file, and reload them later on should one wish to revert? I don't want to simply load in my own defaults in case someone has previously changed them. – gonzobrains Jan 08 '15 at 19:15
  • 14
    Can anyone provide an explanation as to what this series of magic numbers means? – Neil Traft Oct 06 '15 at 22:03
  • 3
    @Neil Traft I havent looked into this exact configuration too much but from my understanding of input event systems when you have an input device like a mouse, it sends back standard input events when buttons are pressed these events tell you that "a button changed state", "that button has id X", and "it current state is pressed/released" normally gtk modifies the input events it receives to map buttons ids [0,1,2,3,4,5,6,7,8] to [1,2,3,4,5,6,7,8,9] keep in mind only the first 3 button ids are used on most mice to indicate the button id for left, middle and right. so middle is now id 25 –  Nov 03 '15 at 14:09
  • Just disabled the middle click at all. Doesn't work. – Artem Dumanov May 30 '23 at 12:10
25

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.

Marco Ceppi
  • 48,101
  • 5
    Any chance for a script that just clears the buffer instead of cancelling all selections? – levesque Oct 29 '10 at 20:16
  • 3
    while(true)? Looks like it will kill the CPU :) – umpirsky Jul 08 '11 at 10:54
  • I've tried mapping butons by adding Option "ButtonMapping" "1 1 3 4 5" to xorg.conf, but the problem resists. – umpirsky Jul 08 '11 at 11:32
  • 9
    @umpirsky The while(true) is not a problem because the loop contains sleep 0.5 which relinquishes half a second of CPU time in each of the loop's iterations. Because of that (and the lightweightness of the xsel command invocation which comprises the other part of the loop), the CPU resources taken up by the loop will be exceedingly tiny even on the slowest of Ubuntu machines. – Eliah Kagan Nov 28 '11 at 07:16
  • 6
    This doesn't seem to work well on Unity because the script clears any existing selection in a terminal, which means you cannot copy test from a terminal by any means (by the time you try to copy, the text is unselected). – Fantius May 18 '12 at 18:45
  • Oh, it's possible. Very VERY possible. (One way is via GNOME Tweak Tool, as seen in an answer below.) – CodeMouse92 Apr 19 '17 at 01:51
  • 2
    @CodeMouse92 No, there is no everywhere-in-X11 way. The Gnome has its own setting, the Firefox has its own setting (i.e. my Firefox does paste on middle button when Gnome has it disabled), the LibreOffice has its own setting, etc. – kubanczyk Mar 27 '19 at 12:26
  • This worked wonderfully: https://unix.stackexchange.com/a/277488/178870 now I no longer have MMB to paste, but MMB to close windows and tabs etc works just fine – axolotl Feb 09 '22 at 20:55
22

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.

  • 1
    Thanks for the info. It explains a little more about the mouse buttons and the actions applied to them. However, it isn't the solution I am looking for. If you left-click a tab in Chrome (to use my example) it selects it (if not selected already). Clicking it with the middle button, will close it (i.e. without clicking the X). I also miss middle-clicking a link to have it open in a new tab for later reading. I realise I can overcome these with alternatives but I am used to this way of working.... – neildeadman Jan 12 '12 at 14:18
  • 1
    Just a thought; any of the other buttons on your mouse, mine say I have 18 buttons though I have yet to physically find more than about 8 of them) may be the function you want. Try mapping other buttons to your third button and see if you find something useful. – Wes Miller Jan 13 '12 at 13:04
  • 2
    If you remap as a two button mouse, it shouldn't change the wheelfunction since the wheel rolls are pressing button 5 and 6 (or 7 and 8 or something like that). – Wes Miller Jan 13 '12 at 13:09
  • 2
    I can confirm that this works on both the built in ThinkPad trackpoint and the Lenovo keyboard that has the same layout. The get-button-map on the Lenovo keyboard comes back with 22 buttons for me, but the same set-button-map <<name-or-id>> 1 0 3 approach works for me – Milimetric Aug 17 '15 at 14:44
  • this is the answer (though I think your description is off) - get-button-map links the position in the list to the physical button, which is the opposite of xmodmap, which links the position in the list to the function. Using the set-button-map, we can map two physical buttons to one logical position. Works on an asus zenbook pro for me. – zzzeek Sep 07 '16 at 15:01
  • 4
    OP did not want to set the middle mouse button to do the same as button1, as that will disable expected middle-click functionality (in his words: "tab-closing, opening links into a new tab automatically, etc"). – Marc.2377 Sep 17 '19 at 03:31
18

For a solution to the problem, please view this guide I wrote:

A while ago, I wrote a patch to disable the 'middle mouse button paste' functionality in GTK. I thought that there would be others who want to disable it as well, and hence I thought I should write a small guide to explain how this feat can be accomplished.

Now, some may ask, why would anyone want to disable it? There are a few reasons:

  • The middle mouse button doesn't actually paste the so-called XA_CLIPBOARD clipboard, but the XA_PRIMARY clipboard. This is probably counterintuitive to many (users coming from Windows, perhaps), and therefore some may view it as more user-friendly to disable the "inconsistent" or unexpected behavior.
  • Some may accidentally paste text using the middle mouse button, and want to avoid that.

A bit of background regarding the XA_CLIPBOARD and XA_PRIMARY clipboard: The XA_PRIMARY clipboard is used mostly for storing selections. Whenever you select some text in for example the GNOME Text Editor (gedit), this text is copied to the XA_PRIMARY clipboard. This text is not pasted when you use the 'Edit -> Paste' menu item, only when you click the middle mouse button. The XA_CLIPBOARD clipboard is mostly used when one uses the general 'Copy/Paste' functionality (through keyboard shortcuts, such as CTRL+C and CTRL+V, or through the menu items 'Edit -> Copy' and 'Edit -> Paste').

Perhaps a patch of this sort (or more drastic changes to the X clipboard and/or how libraries/applications use it) could some day become standard in Linux. I realize opinions on this differ greatly. However, for people who seek to minimize the chances of accidentally pasting some random text, the patch can be pretty useful. For example, with the patch, you can't accidentally paste (at least, with the middle mouse button) text into a document you are editing, or into a web page, or into an instant message, etc.

The guide is for Debian or Debian-based Linux distributions such as Ubuntu and Mint.

What follows are terminal commands with a brief description of what they do (the lines starting with # are comments, which contain these descriptions). You should start a terminal and enter the commands one by one, after carefully reading the descriptions.

# This is a small guide that explains how to patch GTK so that the middle mouse
# button doesn't paste text anymore.

NOTE:

The below instructions are for GTK2. However, they should be easy to adapt

for GTK3 (at the time of writing, the patch works fine for GTK3 too).

First, update the system by first synchronizing the package index files, and

then upgrading the packages.

sudo apt-get update sudo apt-get upgrade

Get the build dependencies and essential packages needed in order to compile

code and create packages.

sudo apt-get build-dep libgtk2.0-0 sudo apt-get install build-essential

Create a temporary directory, in which we will store the GTK sources and

later on the packages.

mkdir /tmp/gtk cd /tmp/gtk

Download the actual patch that will disable the 'middle mouse button paste'

functionality (it should be stored in the directory '/tmp/gtk', and will be,

if you indeed executed the command 'cd /tmp/gtk').

wget http://subversion.assembla.com/svn/slipstream/patches/gtk_disable_middle_mouse_button_paste.patch

Retrieve the GTK sources.

apt-get source libgtk2.0-0

You should adapt this line so that it changes to the correct directory (the

name of the directory that I used here will probably not match the name of

the directory that was created during 'apt-get source libgtk2.0-0', as it

contains a version number that often changes). You can find out what the

correct directory is by entering 'ls -d */' (without the quotes) and looking

at the names of the directories that it shows.

cd gtk+2.0-2.20.1

Apply the patch that we downloaded earlier.

patch -p1 < /tmp/gtk/gtk_disable_middle_mouse_button_paste.patch

The output of the previous command should be:

patching file gtk/gtkselection.c

If it wasn't, then something went wrong. Maybe you mistyped something, maybe

the current directory isn't the correct directory, maybe the GTK sources

were changed and the patch doesn't work anymore, etc.

Build the package (you may have to be patient, this may or may not take a

while).

dpkg-buildpackage -uc -us --source-option=--auto-commit

You should adapt this line so that it installs the correct package. The

package that we want to install is the package containing the GTK library,

thus not the 'bin', 'udeb', 'common', 'dev', or 'doc' package. To find out

what the exact package is that you should install, try to find the package (a

file with a name ending in '.deb') which is closest to the example filename I

used here (the packages are stored in '/tmp/gtk', and you can list the

packages using the command 'ls /tmp/gtk/*.deb' (without the quotes)).

sudo dpkg -i ../libgtk2.0-0_2.20.1-2_i386.deb

And lastly, to make sure that only the patched library is in use, you should

either log out and back in, or restart your computer.

And then, the 'middle mouse button paste' functionality should be disabled.

To test whether it is, try selecting some text in the GNOME Text Editor, or

in a GNOME Terminal, and then press the middle mouse button while the cursor

hovers over some place where you can normally type text. If indeed no text

appears, then it appears that the patch worked.

If however, the patch did not work, try to re-read this document, to see if

you made any mistake. And if you did, you may want to either start all over

again (should be fail-safe), or continue with the guide from the point where

you made a mistake.

Or, more directly, here's the patch to disable the 'middle mouse button paste' functionality in GTK:

diff -ur gtk+2.0-2.20.1/gtk/gtkselection.c gtk+2.0-2.20.1-patched/gtk/gtkselection.c
--- gtk+2.0-2.20.1/gtk/gtkselection.c   2010-05-01 22:14:29.000000000 -0500
+++ gtk+2.0-2.20.1-patched/gtk/gtkselection.c   2011-09-17 10:45:37.000000000 -0500
@@ -1065,6 +1065,24 @@
   display = gtk_widget_get_display (widget);
   owner_window = gdk_selection_owner_get_for_display (display, selection);
  • if (selection == gdk_atom_intern("PRIMARY", TRUE)) {
  •  GtkSelectionData selection_data;
    
  •  selection_data.selection = selection;
    
  •  selection_data.target = target;
    
  •  selection_data.type = gdk_atom_intern(&quot;STRING&quot;, TRUE);
    
  •  selection_data.format = 8;
    
  •  selection_data.data = (unsigned char *)&quot;&quot;;
    
  •  selection_data.length = 0;
    
  •  selection_data.display = display;
    
  •  gtk_selection_retrieval_report(info, selection_data.type,
    
  •          selection_data.format, selection_data.data,
    
  •          selection_data.length, time_);
    
  •  return TRUE;
    
  • }
  • if (owner_window != NULL) { GtkWidget *owner_widget;

  • I haven't had the time to try it (partly because I no longer have a laptop), but this seems like the solution to my problem :) – levesque Nov 28 '11 at 20:50
  • 2
    I'm using Ubuntu 12.04 64 bit with gtk 2.0-0_2.24.10, and it failed on the dpkg-buildpackage line. It wouldn't build with uncommitted local changes, so I had to run dpkg-source --commit. – Sam King Aug 21 '12 at 14:54
  • 2
    @Sam: Thanks for the heads up. Apparently, the --source-option=--auto-commit option can be passed to dpkg-buildpackage (which is somewhat more convenient as one doesn't have to edit the change log). I've updated the guide to reflect this. – Jelle Geerts Aug 26 '12 at 23:40
  • 2
    I tried this solution, but every time I got to the sudo apt-get build-dep libgtk2.0-0 part I get an error with Picking 'gtk+2.0' as source package instead of 'libgtk2.0-0' E: Unable to find a source package for gtk+2.0 Help? – FCTW Jul 04 '13 at 17:38
  • 2
    @FCTW: I suspect the cause of the problem is that GTK3 has replaced GTK2 in modern distributions. Hence, you'll have to find the package name of the installed GTK3 library, by running a command like dpkg -l | grep libgtk. It's probably something like libgtk-3-0. I have verified my patch to work on early versions of GTK3; hopefully it still works. – Jelle Geerts Jul 12 '13 at 12:51
  • The Unable to find a source package for gtk+2.0 problem can be resolved by adding deb-src lines in addition to deb lines in software sources. – lolmaus - Andrey Mikhaylov Mar 01 '16 at 15:26
  • I tried this solution, every command was successful, but after a reboot I still have the middle click paste! :( libgtk version 2.24.23-0ubuntu1.4 Any suggestions? – lolmaus - Andrey Mikhaylov Mar 01 '16 at 15:28
  • @lolmaus: Your problem may well be due to GTK+ 3 being used by programs, so the programs processing your middle click are not using the code that you're patching. That is, unfortunately, installing and patching GTK+ 2 doesn't help, which is what you seem to have done. – Jelle Geerts Mar 08 '16 at 19:46
  • Is there a patch for GTK+ 3? – lolmaus - Andrey Mikhaylov Mar 09 '16 at 10:16
  • 1
    For Gnome use TWEAK > Keyboard and Mouse > Middle Mouse Paste ( OFF ) -- ubuntu x64 on gnome3 – JREAM Apr 02 '17 at 00:20
  • @JREAM it works, but unfortunatelly it back pasting again after restart. and cannot be disabled again – Ahmad Muzakki Apr 02 '17 at 06:08
  • 3
    This seems to be the only answer actually answering the question. And unfortunately it seems outdated. Any fix that currently works? (Universally I mean. The tweak tool can turn it off in some programs like Gedit, but not in the majority of programs.) – Kvothe Aug 22 '18 at 15:27
  • @Kvothe: I would always prefer a solution that is provided by the system and try to avoid patching the system. In the past it probably was not possible to deactivate this function without patching, but now it is (like described in Sunny127's post). The fact, that the patch doesn't work for newer versions of ubuntu should be reason enough. But Jelle's solution deserves respect. It is well described and elaborated and was probably the best solution at it's time. – jottbe Sep 17 '19 at 12:09
  • @jottbe The point of my comment was that no other answer actually answered the question including Sunny127's post. Note that his solution only turns of the middle button paste in a few programs. System wide it is left untouched (as you notice as soon as you use something else than Gedit or a few lucky other applications). By the way I found the following solution on unix stackexchange, https://unix.stackexchange.com/a/277488/288916, that did work. (I also wrote up an answer here with a bit more explanation of what I had to do to make that solution work.) – Kvothe Sep 17 '19 at 16:32
  • @Kvothe: thank you for the clarification. Didn't know that. I hope it works at least for spyder (python development environment), than I already would be happy. – jottbe Sep 17 '19 at 17:34
  • No answer has been included in the actual question – vanadium Dec 19 '19 at 10:42
  • For those who stumble upon this answer and it doesn't solve their problem check this other answer a few scrolls apart from this one: https://askubuntu.com/a/1144039/230104 . IMO that should've been the accepted answer not this one here. – brett Jan 03 '20 at 16:14
15

The only answer that worked for me was given on https://unix.stackexchange.com/a/277488/288916 Radivarig (All credits go to him). Note that I had to make one change though to make it work:

Install xbindkeys:

sudo apt-get install xbindkeys xsel xdotool

Create a configuration file for xbindkeys ~/.xbindkeysrc with the text:

"echo -n | xsel -n -i; pkill xbindkeys; xdotool click 2; xbindkeys"
b:2

Load the configuration file using

xbindkeys -p

Add this line to ~./bashrc so that xbindkeys always loads on startup.

This is what works for me but what Radivarig suggests is to instead use the line

"echo -n | xsel -n -i; pkill xbindkeys; xdotool click 2; xbindkeys"
    b:2 + Release

This last one does not work for me but if one of the versions does not work I suggest to try the other.

After a long time looking this is the only solution I found to work for me that turns off the middle mouse paste button universally without having to disable the middle mouse button completely.


Note that for the existing up-voted answers either they don't answer the question, instead telling you how to disable the middle mouse button completely, or they give a solution that only works in a few programs (tweak-solution only in gedit and gnome terminal and few others) or they simply say it is impossible.


spawn's answer is of a similar spirit and might also work, I did not see it before I found this solution.

Kevin Bowen
  • 19,615
  • 55
  • 79
  • 83
Kvothe
  • 635
7

Solution for both Wayland and X11 users

This script will disable middle mouse paste, it supports both Wayland and X11, and you will still be able to use the middle mouse button normally:

#!/bin/sh
#Script to disable middle mouse paste; Dependencies: xsel, wl-clipboard

if [ "$XDG_SESSION_TYPE" == "wayland" ]; then wl-paste -p --watch wl-copy -p </dev/null # Usually works. #wl-paste -p --watch wl-copy -cp # 100% Effective, may cause issues selecting text in GTK applications. fi

while [ "$XDG_SESSION_TYPE" == "x11" ]; do xsel -fin </dev/null # 100% Effective, May cause issues selecting text in GTK applications. done

Old Answer (A way for X11 that shouldn't cause issues with text selection in GTK): https://askubuntu.com/revisions/1079832/4

Cestarian
  • 251
5

For Gnome applications you can use gnome-tweaks (new name of gnome-tweak-tool package) under the "Keyboard & Mouse" tab there's the "Middle Click Paste" option or editing directly the org.gnome.desktop.interface/gtk-enable-primary-paste Gnome option.

For KDE applications seems that there's an equivalent solution.

For the whole X (including non Gnome applications) you can install XMousePasteBlock which then has to be running (by the user is enough, no root required) in order to work. This disables completely the middle click paste without disabling the other middle click functions.

5

I suggest using one of these, which work mostly well for me:

using xbindkeys: whenever middle-button is pressed, clear the primary clipboard. At least on my system it is cleared, before the pasting happens. Details: create xbindkeys-config:

xbindkeys --defaults > $HOME/.xbindkeysrc

Paste the following new hotkey:

"xclip -i /dev/null"    
    b:2``

Reload xbindkeys (e.g. killall xbindkeys;xbindkeys). Done.

using xdotool: Clear the clipboard on window focus change (should work with most windowmanagers). Details: Execute the following command:

xdotool search --onlyvisible . behave %@ focus exec xclip -i /dev/null

Note that with this command you can still use the primary clipboard within the same window, or pressing middlemouse onto another window BEFORE focusing it (if you don't have "focus follows mouse", or somthing, activated).

spawn
  • 191
3

For anyone experiencing this while using a JetBrains IDE like PyCharm/IntelliJ/Android Studio/etc - note that these IDEs actually have their OWN "paste on middle click" setting which you can disable in settings.

You may have disable middle-button BOTH in the IDE AND using whatever you find here to get it to work.

Peter
  • 181
  • Thanks, you pointed me to the right direction. It's handy to have middle click to paste elsewhere in the system except Intellij. But currently this setting is in other place: Settings -> Keymap -> Editor Actions -> Right click 'Paste from X clipboard' -> 'Remove Middleclick'. And also it's handy to bind "Middleclick" action to "Go to declaration or usages". So you middle click on the class to see its usages. – chill appreciator Jan 03 '24 at 11:11
  • For me also it was good enough to disable middle click paste in Sublime Text. Preferences / Mouse Bindings / copy all the button1 bindings / paste and update to button3 so middle click will behave same as as left click (handy for touchpad) or just override single entry for button3 to have "noop" command to do nothing (for sensitive mouse wheel issue) – Yuri Gor Jan 09 '24 at 09:31
3

The best way I've found so far is to use EasyStroke, which can globally intercept middle-button click and allow to behave as middle button only in certain apps.

You can add a "group" in EasyStroke to apply this interception in multiple applications at once. I've set to disable middle click in some of my text editors, IDE and MATLAB only and works as intended.

Reference: http://ubuntuforums.org/showpost.php?p=11811126&postcount=25

Complete EasyStroke How-To: http://sourceforge.net/apps/trac/easystroke/wiki/Documentation

dbdq
  • 137
  • Thanks from the future for that tip. At first, I was going to just use xinput to disable it totally, and not need to run some extra program, but EasyStroke made it possible to set groups where I could disable the middle button for certain apps (my editor, mainly), and add gestures to certain other apps and using the middle button. So far, works well (14.04 Gnome fallback). – Marty Fried Aug 01 '15 at 18:30
  • Added to my comment: after using EasyStroke for a while, I've found that it often fails to block the middle click. Since I haven't been using the gestures, I am abandoning this program for xinput. – Marty Fried Aug 19 '15 at 16:26
3

the link below fixed the problem for me.

https://wiki.ubuntu.com/X/Config/Input#Example:_Disabling_middle-mouse_button_paste_on_a_scrollwheel_mouse

the page refered by the above link has a section for how to disable the middle mouse paste on scrollwheel, by executing few commands the user can fetch the mouse buttons mapping and can also change the mapping. as explained in the page i disabled the the middle button by executing the command:

$ xinput set-button-map 4 1 0 3
Ahmad Bawaneh
  • 145
  • 1
  • 11
2

This middle mouse button paste behavior is a feature of the X server (and gpm on the text console) and as far as I know at least X.org can't be configured to disable it - all you can do is to change the mapping of the physical mouse buttons as others already suggested.

Chances are good that you can configure your touchpad to avoid unwanted middle clicks, see gpointing-device-settings (not installed by default) or the synaptics manpage if you prefer to use your editor for configuration.

1

In KDE Plasma 5.20 I had to disable both using this answer and klipper (System Tray Settings -> Disable klipper)

Now middle-click pasting is disabled but the but button still works for closing tabs etc.

Dionysius
  • 337
1

On Kubuntu an additional step may be required to solve the problem.

It seems that Klipper, the clipboard manager provided by KDE, breaks the scripts that fix the behavior by clearing the clipboard selection.

The following will globally disable paste on middle click while retaining all middle mouse button and ctrl+c/v functionality.

Follow the steps described in this answer, that is:

  1. Install xbindkeys xsel xdotool

  2. Place this in ~/.xbindkeysrc

    "echo -n | xsel -n -i; pkill xbindkeys; xdotool click 2; xbindkeys"  
    b:2 + Release
    
  3. Reload xbindkeys -p

In step 2. you may need to remove the + Release part as described in this post, depending on what works on your machine.

Set up xbindkeys to run on startup.

Then open Klipper, e.g. via the clipboard icon in the system tray > right click > Configure Clipboard. Uncheck the option 'Prevent empty clipboard'. Reboot and the problem should be solved.

The latter idea is thanks to milaq's XMousePasteBlock.

1

I had the same problem a few months ago and I solved it by changing the mouse! But, as you, I was frustrated that simple button mapping problem can't be solved elegantly by a software fix. Fortunately, I had the problem on my job computer, and my employer owns a variety of spare mouse controllers. It was a no cost fix!

I think a proper mouse hardware implementation should not send random middle clicks while scrolling. Recently I found this behaviour to get annoying even while using Windows!

Now that I've fixed the hardware bug with the proper hardware solution (change the mouse) I even started an addiction to "paste on middle-click" behaviour!!

Happy linuxing!

Sincerly,

1

SW: Ubuntu 14.04, with Gnome fall back. HW: I have a laptop and so the middle button is actually the mouse on/in the laptop. Solution: Go to Ubuntu SW center and download Unity Tweak Tool. Start Tweak Tool. Under the Mouse settings it has switch to turn on/off the middle click insert. Have a lovely day.

user2712329
  • 29
  • 1
  • 6
  • I have the same system. I downloaded Unity Tweak Tool and wasted time looking at every setting, but there was no such setting. Perhaps you meant "Tweak Tool", which is, I think, Gnome Tweak Tool. It has a setting, although it doesn't seem to work. – Marty Fried Aug 01 '15 at 16:42
1

I may have a partial answer for you, if you are using a lenovo X... with touchpoint/touchpad. There is a "known" bug with the synaptics mouse buttons. If you try a USB mouse and have no problem, but with the touchpad/touchpoint mouse (build into the keyboard) you do have random responses that can delete swaths of text as you type, then this may be the bug I'm talking about.

https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1506817

If this fits your situation, then add your name to the list of people who have identified this bug, at that bug report. Maybe if there are many more of us reporting this, it would get fixed.

0

The only working solution I know is to disable copy on text selection instead:

Clone https://github.com/CyberShadow/hax11, read the docs (about NoPrimarySelection)

Build hax11, add preload of hax11.so using export LD_PRELOAD=<PATH_TO_HAX11.so> (see repo docs), add

Enable=1
NoPrimarySelection=1

to ~/.config/hax11/profiles/default

See original answer

d9k
  • 180
  • 1
  • 10
0

I tried the xinput-redirection trick, changing the center mouse "button" (actually a wheel) so it acts just like the left mouse button. It still works as a wheel, and has (apparently) stopped pasting things into random places in the middle of my source code as I scroll past.

In my case the command was

xinput set-button-map "PixArt USB Optical Mouse" 1 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16

but YMMV by mouse model.

muru
  • 197,895
  • 55
  • 485
  • 740
0

You might want to try emulating a two button mouse. With a two button mouse you paste by clicking both mouse buttons at the same time (rather then the scroll wheel).

Install gpointing-device-settings:

sudo aptitude install gpointing-device-settings

http://live.gnome.org/GPointingDeviceSettings

Alternately, if you do not wish to install gpointing-device-settings , and you are not bothered by command line options, see https://wiki.ubuntu.com/X/Config/Input On this wiki page there are several command line / configuration options, choose the one you prefer.

Panther
  • 102,067
-1

Contrary to the title, the 10-year old problem seems to be really

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.

Also, you can't safely write programs.

Less destructive than "to disable the middle mouse button paste", I see 3 possible solutions, only the first one effective so far:

1-Exchange buttons 2 and 3

xinput set-button-map <id> 1 3 2

where <id> should be replaced by the mouse id as explained above by Wes Miller.

To make the change permanent, writte in .xprofile. (.bashrc won't work unless you use bash.)

Thus,

  • if you accidentally click button 2 while scrolling, this will only open the alternate action menu, that does nothing harmful by itself,

  • last but not least, you can can still click to paste X buffer with button 3 (right).

The inconvenience is that you need to reverse your clicking habits, which feels like driving on the other side of the road.

2-Find a mouse with a fourth button and remap button 2 to button 4

However, I am unable to certainly identify such a mouse on the market. If you know one for sure I am interested but not a costly wired gaming mouse.

I have tried another 3 button mouse: the wheel click is less sensitive but the problem persists.

3-Use middle button emulation

% xinput list-props <id>

This should return something (among others) like

libinput Middle Emulation Enabled (353):    0

Activate middle button emulation:

xinput set-prop 15 353 1

Now, you also need to deactivate button 2. Remapping to 0 or 25 won't work because it will also deactivate middle button emulation. This kills solution 3.

Tested on Ubuntu 20.04.

-1

To solve this issue, I went to the Mouse option by searching it in the menu bar, then I saw Enable middlemouse pate and I disabled it.


[NOTE]:

I'm using Ubuntu Mate 22.04


[UPDATE]:

enter image description here

-1

Using what I learned in the posts above, this bash one-liner works perfectly for me...

mouse_id=$(xinput list | grep 'Mouse' | awk '{print $9}' | sed 's/[^0-9]//g') && xinput set-button-map "$mouse_id" 1 0 3
Scott
  • 109
  • 1
  • 3
    The OP specifically mentioned he did not want to completely disable the middle click button. – Uyghur Lives Matter Aug 25 '14 at 14:10
  • I modified your one-liner to support multiple mice: xinput list | grep 'Mouse' | grep -o 'id=[0-9]*' | grep -o '[0-9]*' | xargs -i xinput set-button-map "{}" 1 0 3 – Kurt Oct 23 '18 at 21:04
-1

That's a good question, which i don't have an answer for (yet). A quick and dirty workaround is to remap it NOT to 0, but to 1. This way, it turns middle-"click" to left click, and does not affect your scroller... It is so far the best I can think of.

Note:This information came from Ubuntu Forums, not my own noggin! :)

Tiede
  • 169
-2

It's more than a gnome feature, i think it works almost everywhere, it works in the console too, and I think it worked even in my "Linux from Scratch".

So it's really a basic feature perhaps even somewhere in the kernel.

BTW: It's really useful, and it's not the regular paste like Ctrl + V, everything that is marked with the cursor goes in a second storage and with middleclick can paste, what you marked last.

phiphi
  • 17
-3

did you check out gpm ? More info at http://manpages.ubuntu.com/manpages/lucid/man8/gpm.8.html. Available via sudo aptitude install gpm on lucid. I don't see the disable-paste program in the ubuntu package however, the -A option may be worth giving a try.

koushik
  • 5,072
  • 5
    I don't understand gpm.. in the package description they state: "This package tries to be a useful mouse server for applications running on the Linux console."

    What does this have to do with applications running in windowed mode?

    – levesque Sep 21 '10 at 16:35