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.
This solution will work globally and preserve the middle mouse functionality under Xorg.
xbindkeys xsel xdotool
Place this in
"echo -n | xsel -n -i; pkill xbindkeys; xdotool click 2; xbindkeys" b:2 + Release
xbindkeys on startup,
pkill xbindkeys to stop.
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
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 )
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 -
Edit: In my opinion the following answer has a better solution: https://unix.stackexchange.com/a/501445/72554
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.
For some unknown to me reason,
echo -n | xsel -n -i may not work, so you can try replacing all commands with
echo -n | xsel -z
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.
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.
Expanding the @flarn2006 answer, I have created a daemon switch and a simple eraser:
notify-send, and a shortcuts manager (in my case is
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
sh $HOME/.middle-paste/toggler.shto a
SUPR + Xshortcut to quickly toggle the daemon. Please substitute the
$HOMEvariable with your real home path or the Shortcut Manager could fail finding the script (eg.
xsel -cto a
SUPR + Vshortcut 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
Inkscape, where this issue is problematic only when there are something in the clipboard already).
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:
- Unplug the mouse from the USB port.
- Go get a #1 phillips screw driver and a pair of electronic side-cutters
- Look under the mouse and find the screw to open the bottom cover.
- Remove the screw and remove the cover.
- Find the little microswitch that is underneath the spring-loaded scroll wheel arm.
- 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.
- Clean out the debris and re-assemble the mouse.
- Plug the mouse back into the USB port.
- 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!
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:
xbindkeys xsel xdotool
Place this in
"echo -n | xsel -n -i; pkill xbindkeys; xdotool click 2; xbindkeys" b:2 + Release
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.
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
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.
I found yet another solution using
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 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.
sudo apt install sxhkd
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
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.