91

I don't like having the middle mouse button paste, because I often end up with uncompilable code in Eclipse. How can I turn this off (in all programs)? I'm running Fedora.

5
  • 5
    Middle-click to insert selection is fundamentally part of the way X11 works; see http://www.jwz.org/doc/x-cut-and-paste.html.
    – ephemient
    Nov 10, 2011 at 22:44
  • 4
    I've not seen any trivial way to do this without nuking the middle mouse button altogether or hacking X or screwing with the normal clipboard as well, so my suggested "solution" is just to learn to use the middle mouse button properly. It's incredibly convenient to be able to highlight a text URL anywhere and go to it by middle clicking in my (FF) browser window.
    – jw013
    Nov 10, 2011 at 23:51
  • 13
    @ephemient "Middle-click to insert selection is fundamentally part of the way X11 works" ... way back when we had Sun Optical Mouses that needed about 1 pound of pressure on the middle button to get signal. Today the middle button is on hair trigger and moreover used as a wheel, this "fundamental" has to go (as quite a lot of "fundamental" stuff in X btw.) Feb 15, 2018 at 14:44
  • 19
    I feel like the ability to disable whatever you want is an even more fundamental aspect of Linux. Apr 5, 2019 at 16:29
  • Step 1: write complex software for 5h, step 2: try to complie.. Ooops, something goes really wrong.. Step 3: check your code for 1h.. Step 4: Find you inserted code in a header by pressing middle-click. Conclusion: Middle click paste needs to go! Nov 30, 2021 at 14:24

10 Answers 10

77

This solution will work globally and preserve the middle mouse functionality under Xorg.

  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

Run xbindkeys on startup, pkill xbindkeys to stop.

11
  • 6
    @spiil I tried this solution and it achieves the effect you want. I middle-clicked in a text field and nothing happens, but middle-clicking on a link and middle-clicking to close a tab work in Chromium, Firefox, and Waterfox (the three browsers I tested). Jan 7, 2019 at 21:53
  • 7
    Scrolling by click-and-hold middle button no longer works with this solution :/
    – Marc.2377
    Mar 10, 2019 at 3:36
  • 1
    In order to get this to work I had to remove "+ Release". So if people are having trouble with the above solution give that a try. (And maybe this can be added to the answer).
    – Kvothe
    May 17, 2019 at 13:00
  • 1
    Wouldn't that have the side effect of also killing every running process of yours with "xbindkeys" in the name? Might seem kind of far fetched for there to be any such process running other than the one you want to kill, but what if you have an xbindkeys-related script (perhaps even this one) where you put that in the name? I wonder if using a systemd service file and systemctl --user stop instead would be efficient.
    – flarn2006
    Oct 26, 2019 at 22:10
  • 1
    nowadays I use github.com/milaq/XMousePasteBlock, which appears to use the same basic technique as this, but packaged nicely.
    – Retr0id
    May 31, 2020 at 2:02
23

In Fedora 24 you can install gnome-tweak-tool using the following command:

$ sudo dnf install gnome-tweak-tool

Open gnome-tweak-tool and go to the "Keyboard and Mouse" tab and disable "Middle-click-Paste".

5
  • 6
    This is also available in Ubuntu 16.04: sudo apt-get install gnome-tweak-tool Feb 9, 2017 at 22:55
  • 9
    This only affects a few programs such as gedit, gnome-terminal and a few others. See also comment at askubuntu.com/questions/4507/….
    – Kvothe
    May 17, 2019 at 12:32
  • 1
    In Fedora 33, while disabled in tweak tool, middle click paste still is enabled in google chrome. Mar 20, 2021 at 1:30
  • after updating my Gnome extensions today its not working
    – caduceus
    Nov 16, 2021 at 15:50
  • Doesn't do anything on my Ubuntu 20.04
    – MERose
    Jan 30 at 21:16
22

Scrollwheel mice support a middle-button click event when pressing the scrollwheel. This is a great feature, but you may find it irritating. Fortunately it can be disabled.

First, you need to know the id of the mouse, like this:

$ xinput list | grep 'id='

which prints something like

⎡ Virtual core pointer                      id=2    [master pointer  (3)]
⎜ Virtual core XTEST pointer                id=4    [slave  pointer  (2)]
⎜ PixArt Dell MS116 USB Optical Mouse       id=12   [slave  pointer  (2)]
⎣ Virtual core keyboard                     id=3    [master keyboard (2)]
  Virtual core XTEST keyboard               id=5    [slave  keyboard (3)]
  Power Button                              id=6    [slave  keyboard (3)]
  Video Bus                                 id=7    [slave  keyboard (3)]
  Power Button                              id=8    [slave  keyboard (3)]
  Sleep Button                              id=9    [slave  keyboard (3)]
  Dell KB216 Wired Keyboard                 id=10   [slave  keyboard (3)]
  Dell KB216 Wired Keyboard                 id=11   [slave  keyboard (3)]
  Eee PC WMI hotkeys                        id=13   [slave  keyboard (3)]

My Dell Usb mouse has printed here for which id=12

so, I can view the my mouse button mapping like:

$ xinput get-button-map 12

which prints

1 2 3 4 5 6 7 8 9 10 11 12

here only the first three numbers have meaning for me. They represent the left, middle, and right mouse buttons.

I can turn the middle mouse button off by setting it to 0:

$ xinput set-button-map 12 1 0 3

Or I can turn the middle-mouse button into a left-mouse button by setting it to 1:

$ xinput set-button-map 12 1 1 3

ref. link https://wiki.ubuntu.com/X/Config/Input

it works for me ( kubuntu 18.04 LTS )

4
  • 2
    This is a better than any of the above that start with "download XX". The only issue I had is that for xinput set-button-map 12 1 0 3 you set all the buttons (1 - 12 for in this exampe xinput set-button-map 12 1 0 3 4 5 6 7 8 9 10 11 12)
    – blindguy
    Aug 6, 2019 at 0:34
  • Note that you can find which button is which by pressing it and holding it down (then deleting any pasted text) then (with it still held down), type xinput query-state 12 (or whatever xinput id your mouse is, if it isn't 12), you can then see that only that button is down and you can disable it. I'm pretty sure this will disable middle mouse button functionality everywhere, not just for pasting, but this is what I wanted, I never use the middle mouse button because it's too sensitive. This does not affect the mouse wheel, though.
    – jrh
    Apr 6, 2020 at 15:55
  • Note that xinput list | grep 'id=' has a different id at each reboot. So adding something to the bash start up script would need to process this. I don't have the bash script-fu skills to do this. Maybe someone does and can provide a link to their script?
    – RyanNerd
    Jun 6, 2021 at 6:25
  • This kills middle click, not middle click paste.
    – lemontree
    Mar 4 at 3:15
12

Having the middle button paste is a unix user interface standard, like having the left button select or activate, and the right button do something else (such as extending, toggling, firing up a menu, …). You'll find it bound to pasting in most unix applications.

If your problem is that your mouse is overly sensitive when you put your finger on the wheel, I suggest getting a better mouse. There are plenty of cheap decent mice.

You can reassign mouse button 2 to a different number. Applications identify the left, middle and right mouse buttons as buttons 1, 2 and 3 respectively. Put this code in a script that's executed when your X session starts (how to do that depends on your desktop environment).

echo 'pointer = 1 6 3 4 5' | xmodmap -
7
  • 8
    I'm betting OP doesn't care if it is standard (I know I don't). Doesn't matter. Mouse button actions should be more easily customizable.
    – Marc.2377
    Mar 10, 2019 at 3:30
  • Good answer. In my case, a variant is need: echo 'pointer = 1 0' | xmodmap - Mar 4, 2020 at 19:43
  • 1
    "I suggest getting a better mouse" -- Unfortunately mice both cheap and expensive tend to have hair trigger mouse wheels, I don't know why. My 50 USD gaming mouse that's otherwise very good will happily middle click while scrolling with slightly more vigor than usual. It's not even a distinct click, it's a mushy sort of squish click, so I don't even know it happened. As a lefty unfortunately I don't have a lot of choices. This was the only mouse I could use in the store with more than 3 buttons.... it's not just me either, colleagues using standard Dell mice had issues with this too.
    – jrh
    Apr 6, 2020 at 15:58
  • 1
    @jrh My favorite mouse has a thumb button. It also has a wheel that's good enough not to be pressed by accident, but I rarely press it, because I use the thumb button. I doubt they make it anymore: it must be about 20 years old. It's very asymmetric though, I don't know if a left-handed version exists, that does unfortunately limit choices a lot. Apr 6, 2020 at 19:17
  • Funny enough until I found Suraj's answer I was seriously considering disabling middle click everywhere through a driver if available, or in the worst case, mechanically altering the mouse to try and prevent the button from even functioning, luckily it didn't come to that.
    – jrh
    Apr 9, 2020 at 14:38
11

Here's what you can do if you want to keep middle click functional (for clicking to open links in new tabs for example) without it pasting things.

First install sxhkd and xsel.

Then configure ~/.config/sxhkd/sxhkdrc like so

~button2
    echo -n | xsel -n -i
~control + c
    echo -n | xsel -n -i
~control + shift + c
    echo -n | xsel -n -i
~control + x
    echo -n | xsel -n -i

And that's basically it.

Now start sxhkd (or configure it to launch on startup). Now, middle mouse will no longer paste anything but will otherwise function like normal.

1
  • 1
    This still lets you use middle mouse for scrolling. Good solution! For me the file was in ~/.config/sxhkd/sxhkdrc
    – Atnas
    Sep 10, 2019 at 15:49
5

EDIT: I just found an issue with the solution I've posted: in some programs (gitk at least, not sure how common it is) this will prevent text selection from working. So try it, but if you have problems with selecting text in a program you use regularly, then the solution is to stop using this.

Install xsel, and then run the following shell script:

#!/bin/sh
while true; do
    xsel -fin </dev/null
done

As long as this script is running (you can set it to run on login) the middle mouse button will not paste in any application. So technically it will still paste, but there will always be nothing to paste.

The way it works is it runs xsel and has it copy (-i) the contents of /dev/null, without forking to the background (-n), and to actually hold that empty string in the buffer instead of just clearing it and exiting (-f). Every time it exits, it will invoke xsel again with the same options. Since xsel will keep running until another program "relieves it of duty" by copying something else, it won't constantly create new processes, instead keeping a single process running precisely until it is needed again. (Unless you have multiple instances of the script running, but there's no need to do that.)

Also no, this won't interfere with regular copy/paste, because the middle-click paste uses a separate clipboard.

0
1

It's a common convention that many programs abide by. There is no global way to turn it off, you must do it on a program by program basis.

EDIT: (short of, as Gilles says, disabling mouse button 2. But some programs do have other uses for mouse button 2, so I would not recommend this.)

1

Expanding the @flarn2006 answer, I have created a daemon switch and a simple eraser:

Requirements

xsel, notify-send, and a shortcuts manager (in my case is xfce4-keyboard-setting)

First create a .middle-paste/ folder in $HOME and add two files:

#!/bin/sh
# toggler.sh

# Set the lock file and program paths
PROGRAM_PATH="$HOME/.middle-paste/"
LOCKER_FILE="$PROGRAM_PATH/lock"

# Check if there is a lock file
if [ -f "$LOCKER_FILE" ]; then
    # If exists, read the PID of the daemon, which is contained inside the file
    daemon_pid=$(cat $LOCKER_FILE)

    # Then kill the daemon
    kill "$daemon_pid"

    # And delete delete the lock
    rm $LOCKER_FILE

    notify-send --urgency=critical --expire-time=500 \
    'Middle Mouse Paste Disabled' \
    'There could be some troubles handling the middle mouse button in some apps.'
else
    # If the file does not exist, create it
    edit > $LOCKER_FILE

    # Then run the daemon to disable middle click pasting
    sh $PROGRAM_PATH/daemon.sh &

    # A little pause
    sleep 0.1s

    # Get the daemon PID to show it in the notification
    daemon_pid=$(cat $LOCKER_FILE)

    notify-send --urgency=critical --expire-time=500 \
    "Middle Mouse Paste Enabled ("$daemon_pid")" \
    'There could be some troubles selecting text in some apps.'
fi
#!/bin/sh
# daemon.sh

# Set the lock file and program paths
PROGRAM_PATH="$HOME/.middle-paste/"
LOCKER_FILE="$PROGRAM_PATH/lock"

# Append the PID of this process the lock file
echo $$ > $LOCKER_FILE

# Run the daemon
while true; do
    xsel -fin < /dev/null
done

Second configure the switcher

It can be a launcher, a button in your panel, or even a shortcut, you only need to

  1. Add sh $HOME/.middle-paste/toggler.sh to a SUPR + X shortcut to quickly toggle the daemon. Please substitute the $HOME variable with your real home path or the Shortcut Manager could fail finding the script (eg. /home/user/.middle-paste/toggle.sh)
  2. Add xsel -c to a SUPR + V shortcut to clear the middle mouse clipboard quickly without the need of a daemon. This is useful when you know that you will not get out of the application where you will not copy, but you will use the middle mouse button (Like Figma or Inkscape, where this issue is problematic only when there are something in the clipboard already).
0

With KDE 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 Radivarig's 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.

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 idea of the latter is thanks to milaq's XMousePasteBlock.

0

If you are using KDE desktop manager, KDE Plasma 5.18 for example, you could open Configure Desktop in right-click menu at desktop window. Then check Mouse Actions to configure/remove the action for Middle-Button.

You can also refer to this tutorial for more information: Detailed Guide to Configure KDE Plasma Desktop.

PS: I have been puzzled over the default paste action of middle button for a long time. It is a bit weird that such configuration cannot be accessed in System Settings. And it is just be discovered accidentally. Hope it would help you if you are using KDE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.