Skip to content

Instantly share code, notes, and snippets.

@ItsProfessional
Last active April 21, 2024 11:46
Show Gist options
  • Save ItsProfessional/20c01cfdfcecb00adefd4d12ee4b1239 to your computer and use it in GitHub Desktop.
Save ItsProfessional/20c01cfdfcecb00adefd4d12ee4b1239 to your computer and use it in GitHub Desktop.
Disable middle click pasting in Linux

Disable middle click pasting in Linux without breaking middle click functionality (X11/Wayland)

Context

  • Both X11 and Wayland have clipboard and primary clipboards. The clipboard clipboard stores the text you copy. The primary clipboard stores the text you select (not copy!) using the mouse.
  • When you paste using your keyboard, it pastes the text stored in the clipboard clipboard, but when you paste using middle click, it pastes from the primary clipboard.
  • The idea is to clear the primary clipboard, when you middle click so that nothing gets pasted, and we only clear the primary clipboard so the text you copied previously will remain as-is.

Clearing the "primary" clipboard on middle click

  • Install a key binding daemon that is capable of listening to mouse button clicks (middle click in our case) and executing shell commands. I personally recommend evsieve.
  • If you are using X11, make sure xsel is installed. If you are using Wayland, make sure BOTH xsel (for xwayland windows) and wl-clipboard (for native windows) are installed.
  • Run evsieve with these flags on boot (e.g. in your wm/compositor's config):

Note: Replace /dev/input/by-path/your_mouse with the actual path to your mouse.

evsieve --input /dev/input/by-path/your_mouse grab --hook btn:middle exec-shell="wl-copy -pc && xsel -nc" --output
  • Restart your wm/compositor (to ensure all applications restart from a fresh slate)
  • Try pasting with middle click. If everything went correctly, it should no longer paste anything.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment