12

In Linux, clicking with the middle mouse button copies the currently selected text or pastes what was copied previously. It's a separate "slot" than the normal copy and paste. What's worse, it overrides the default middle click behavior. For example in some web apps, pasting whatever text I have selected previously, which is very undesirable.

There are sometimes options in individual programs, such as middlemouse.paste in Firefox, but I'd like a solution that works across the entire system without the need to adjust it individually for every program. How can it be achieved?

8
  • 2
    I'm confused. The "default middle click behavior" is paste :-). it's also kinda hard to do accidentally. Commented May 28, 2024 at 8:59
  • 2
    "it overrides the default middle click behavior" - What do you mean by that exactly? What behaviour would you expect from the middle mouse button? Commented May 28, 2024 at 9:46
  • 4
    I would expect it, for example, to open a link in a new tab, or engage autoscroll in a web browser, or do other things in other programs. It's really unusual for it to end in a paste in any normal program, especially when it hijacks - and replaces - the usual behavior. Commented May 28, 2024 at 21:24
  • Middle-clicking in browsers does open a new tab in Linux. At least, it does in Chromium and Firefox. And in the past, in Galeon, Mozilla, Opera, Netscape, and whatever other browsers I've used. Same for programs like Gimp and Kicad which use the middle mouse button for panning. In my 25 years of using desktop Linux, I've never seen middle-click paste "overriding" anything. Commented May 29, 2024 at 21:38
  • 3
    Well, that's not how it works in any other system and it's a very weird functionality that's got to change. Almost anybody who uses Linux daily has pasted some random text somewhere it shouldn't go at least a couple of times because they middle-clicked inside a text field somewhere. God forbid if there's a newline in there. Normal clipboard uses ctrl+c/ctrl+v and that's way harder to do by accident. Commented May 31, 2024 at 23:49

6 Answers 6

14

without the need to adjust it individually for every program. How can it be achieved?

It kind of cannot, because it is implemented individually within each program. The primary selection buffer is shared, yes, but the actual association of middle-click event with "paste from primary" is not (and neither is standard Ctrl-V clipboard, for that matter). Firefox handles it on its own, GTK-based apps let GTK handle most events, Qt-based apps let Qt handle them, etc.

So, for example, even if you forced the primary selection to remain always empty, it would not change how the apps behave – middle-clicking in Firefox would still lead to it trying to paste from primary instead of doing whatever else you expect it to do. Maybe it will change behavior if the buffer is empty, but more likely it won't.

(And the way primary selection is usually handled, unfortunately, is that toolkits bind its ownership to the literal "selection" (highlighted text) to visualize its contents. So if you had e.g. a clipboard manager that immediately clears the contents of the primary buffer to prevent paste, it would result in text getting immediately deselected in text editors...)

So your best option, most likely, is to find ways to disable middle-click paste at app or toolkit level. GTK3, for example, has an "enable-primary-paste" setting which can be set through its settings.ini or through GSettings:

Gnome

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

Cinnamon

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

Mate

gsettings set org.mate.interface gtk-enable-primary-paste true 

(In an X11 environment the DE exports this as Gtk/EnablePrimaryPaste through XSETTINGS, so if you're using GTK apps outside of a GNOME-based environment, xsettingsd might be used to publish it – or GTK's settings.ini can be edited directly.)

This option was added for GTK3 and later (GTK2 apps will not recognize it). Nothing for Qt either, unfortunately.

8
  • 1
    Firefox has middlemouse.paste in about:config which seems to work Commented Jun 28, 2024 at 7:52
  • The question already stated that Firefox has middlemouse.paste. Commented Jun 28, 2024 at 10:33
  • This doesn't do anything in Cinnamon 6.0.4. I already have the settings set to false,false, true, in the mentioned order, yet middle click still pastes stuff in FF. Commented Jul 11, 2024 at 10:54
  • @Leprechaun: Firefox is not a GTK program, which was the entire point of the post! Commented Jul 11, 2024 at 10:57
  • @grawity_u1686 But the OP was asking about a system-wide solution, even specifically mentions FF. Your answer is misleading, because you end up with your best option, most likely, is to find ways to disable middle-click paste at app or toolkit level. which doesn't work. Commented Jul 11, 2024 at 11:02
1

Just adding: this is a setting "Paste the current selection when middle-click is pressed" in Cinnamon for Linux Mint in mouse settings, but does not work, and apparently has been known to not work for many years.

Mouse settings: Mouse settings

0
sudo apt install xbindkeys xsel xdotool 

Then edit ~/.xbindkeysrc:

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

Check config with xbindkeys --show. Run xbindkeys

If xsel -n -i doesn't work try xsel -z

Found this solution on Reddit.

0

These two commands should disable it on both Wayland and X (or Xwayland apps)

  • wl-paste -p --watch wl-copy -cp
  • bash -c 'while true ; do xsel --primary --clear ; sleep 0.5 ; done'

The downside is that certain GTK/GNOME apps will assume 'primary clipboard = in-app selection' and thus constantly reset your mouse selections. You can take a look at https://askubuntu.com/a/1079832/2075363 for possible workarounds.

0

Here's a script that will reliably disable it on X11/Xlibre and any wlroots based wayland compositor (including but not limited to hyprland)

#!/bin/sh #Script to disable middle mouse paste; Dependencies: xsel, wl-clipboard if [ "$XDG_SESSION_TYPE" == "wayland" ]; then 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 

As the warnings in my comments indicate, this may break your ability to select text in some GTK applications.

If you want a method that does not risk this, you can replace the wayland specific command with this one:

wl-paste -p --watch wl-copy -p </dev/null # Usually works. 

It generally works but in some cases it's not a 100% effective (sometimes a middle click paste may slip thorugh, I believe it won't work on chromium for instance)

A potential workaround is to create a script that detects if the currently active window is one of the problematic gtk applications, then make the script ignore the app (allow middle click pasting).

Because only gtk apps are affected, you can just disable middle click pasting for gtk apps using this command:

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

So those problematic apps won't actually have middle click pasting even if the script isn't the method used to disable it in them.

You can see an example of how this can be done in my unix stack exchange answer: https://unix.stackexchange.com/a/472464/72554

Furthermore, if you're on KDE wayland, you can use the aforementioned gtk middle mouse click disabling method in combination with the kde system setting to control middle click action for Qt: Window Management -> Window Actions -> Middle Click to disable middle click pasting for the vast majority of apps without the use of the script, since it might not work as KDE Wayland is not a wlroots compositor. If you're on gnome you're screwed, you can only disable it for GTK applications on gnome.

-1

You can use Linux Mint Cinnamon Edition.

In its Mouse and Touchpad applet, look in the General section of the Mouse tab. Within that section, you will see a toggle for "Paste the current selection when middle-click is pressed". It's the 5th item from the top. Set that toggle to the off position.

Now middle-clicking will no longer perform the undesired paste operation.

8
  • 1
    Why the downvote, does this not work? It looks specific to Cinnamon, but sounds plausible? Xfce also has mouse and touchpad options but doesn't have anything paste or middle-click related. Actually, maybe the first line could sound like a command: "USE Linux Mint Cinnamon" and maybe that prompted a downvote from someone with a bad taste for Cinnamon Commented May 28, 2024 at 1:10
  • 3
    @Xen2050 Not the downvoter, but I can certainly see why someone's opinion might be that "you can change your entire operating system to get a GUI to configure this" is not a very good answer to the question, if that's how they interpreted "You can use Linux Mint Cinnamon". No need to assume someone is acting out of bias against Cinnamon or whatever. Commented May 28, 2024 at 5:30
  • 3
    @Xen2050: My guess is that someone thought "use this specific distro" is a bit overkill as this isn't really a Mint-specific or even Cinnamon-specific setting; all that it ultimately does is flip a toggle that GTK3/4 already has built-in regardless of distro or desktop. (It has no effect on non-GTK3/4 apps.) It just happens that Cinnamon displays it prominently whereas GNOME hides it in the "GNOME Tweaks" app (or in the "Linux Regedit" dconf-editor if you want). Commented May 28, 2024 at 8:34
  • 1
    Does not work in firefox. It works in the built-in text editor, presumably because it's GTK-3, but not everywhere, like the question asks. grawity's answer seems to hold for Mint too Commented Jun 28, 2024 at 7:49
  • 1
    @Zoe I agree with the first part - that toggle turns the feature off in some apps. But grawity's answer does not help with FF in Mint. Commented Jul 11, 2024 at 10:55

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.