105

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
  • 16
    @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
  • 30
    I feel like the ability to disable whatever you want is an even more fundamental aspect of Linux. Apr 5, 2019 at 16:29
  • 1
    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

12 Answers 12

86

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.

12
  • 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
  • 9
    Scrolling by click-and-hold middle button no longer works with this solution :/
    – Marc.2377
    Mar 10, 2019 at 3:36
  • 2
    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.
    – Sparkette
    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
29

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 )

5
  • 3
    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
  • 4
    This kills middle click, not middle click paste. Mar 4, 2022 at 3:15
  • Killing middle click completely with only standard tools is perfect for the touchpad on my laptop (where much of what feels like the left button is detected as middle). It's also simpler than disabling Firefox's middle click on a tab to close it. So have a great big +1 from me.
    – Chris H
    Nov 14, 2023 at 20:18
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
    – user215253
    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
  • 2
    In Fedora 33, while disabled in tweak tool, middle click paste still is enabled in google chrome. Mar 20, 2021 at 1:30
  • 1
    after updating my Gnome extensions today its not working
    – caduceus
    Nov 16, 2021 at 15:50
  • 1
    Doesn't do anything on my Ubuntu 20.04
    – MERose
    Jan 30, 2022 at 21:16
13

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 '' # 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://unix.stackexchange.com/revisions/472464/6

2
  • 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
  • Thank you! it's working for Manjaro & KDE with echo -n | xsel -z
    – Nikit
    Dec 12, 2022 at 4:18
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
  • 11
    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
10

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.

3
  • This solution is amazing! Seriously, this is the best solution here! It completely blocks middle mouse pasting without interfering with normal pasting at all and it doesn't mess with the middle mouse button's other functionality in any way!
    – Cestarian
    Jun 28, 2022 at 23:06
  • It does work on wayland, but uses a whopping 10% of CPU load on a thinkpad t480s during regular tasks. As I understand it should just run once and then be stuck? I added sleep 1 and could not find any drawbacks to this yet. Wondering why not use xsel -pc instead of copying /dev/null? Mar 30, 2023 at 16:06
  • This will still "paste" the empty string right? So if I select some text and then hit the middle mouse button it will effectively delete the selected text, or not?
    – Kvothe
    Oct 24, 2023 at 16:01
2

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

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.

1
  • works, but with the caveat that it will try to paste an empty text, so if you have something selected, that's gone.
    – My1
    Sep 30, 2023 at 23:08
1

I've been coding for decades but mainly on a Windows machine. Only recently I've been delving into the world of coding under Linux and now I've run into this annoyance - my most recent project was peppered with random text because the middle button on the mouse I was using was rather sensitive - I was totally stumped until decide to google the issue and landed here.

I have never intentionally used the middle button to click anything ever, perhaps because it doesn't do anything in Windows by default. I have only ever used the middle scroll wheel to scroll. After reading a few of the various solutions on the web and in this thread, I've decided on a simpler solution.

Here it is:

  1. Unplug the mouse from the USB port.
  2. Go get a #1 phillips screw driver and a pair of electronic side-cutters
  3. Look under the mouse and find the screw to open the bottom cover.
  4. Remove the screw and remove the cover.
  5. Find the little microswitch that is underneath the spring-loaded scroll wheel arm.
  6. Use the side-cutters to crush that switch and cut off the guts of it. Try not to damage the optical wheel and sensors. Enjoy the feeling of getting your revenge for having to scan through all your code for random bits of text.
  7. Clean out the debris and re-assemble the mouse.
  8. Plug the mouse back into the USB port.
  9. Observe the scroll wheel working. Try to click the middle mouse button - oh look, nothing happens!

Aaaahhhhh! That's much better!

p.s. You can use this opportunity to clean out all the crud that builds up inside your mouse - your mouse will perform so much better now!

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.

1
  • that doesnt actually seem to affect applications sadly
    – My1
    Sep 30, 2023 at 23:07
0

I found yet another solution using sxhkd.

It disables plain middle click (without hold down a modifier key) for all XWayland applications, like Chromium, Konsole, Discord. (Maybe even all applications when using Xorg.)

This fix has no effect on real Wayland applications, like Terminal and Firefox (with Wayland enabled). For these, it is still needed to use something like gnome-tweaks and about-config.

This solution also solves the problem of accidentally opening tabs with random links when using a thinkpad trackpoint keyboard II to scroll on a webpage. Mine literately middle clicks every time I scroll.

Also, with the echo -n | xsel -n -i trick, the cursor position still changes when pasting a empty clipboard. This still can be really annoying when you just want to continue typing where you left off after some scrolling.

Another benefit of this fix is that the middle click paste functionality still works! You just need to hold down one of the modifier keys while middle clicking.

Install sxhkd:

sudo apt install sxhkd

Make a sxhkd config at ~/.config/sxhkd/sxhkdrc (or append if one already exists) to capture middle click, without replaying it to the application:

mkdir ~/.config/sxhkd
# capture plain middle click with the no-op ':'
cat << EOF >> ~/.config/sxhkd/sxhkdrc
button2
     :
EOF

Run sxhkd:

sxhkd

To automatically run sxhkd at startup, it can be added using 'Startup Applications', or use this command:

# run sxhkd at startup (this is the same as doing it manually via 'Startup Applications')
cat << EOF > ~/.config/autostart/sxhkd.desktop
[Desktop Entry]
Type=Application
Exec=sxhkd
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name=sxhkd
Comment=catch plain middle click in non Gnome apps
EOF

If you do a lot of horizontal scrolling by holding down shift, you can also add these lines to your config. Then also middle mouse clicks when holding down shift gets captured.

shift + button2
     :

EDIT: After some time using this solution, I found another really nice feature. Middle click on an unfocused XWayland window now just focuses it, without clicking! So you don't lose selected text or your cursor position, and you don't have to click carefully around all active areas. In Gnome this has the same effect as Super-Click or Alt-Tab.

1
  • For Wayland, swhkd may do the trick also for Gnome applications.
    – StanVL
    Jun 9, 2022 at 11:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .