Skip to main content
Code corrections, improvements, and clarification
Source Link
DATALOT
  • 459
  • 1
  • 5
  • 15
#!/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
    bashsh $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
  1. Add bashsh $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).
#!/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
    bash $PROGRAM_PATH/daemon.sh &

    # 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
  1. Add bash $HOME/.middle-paste/toggler.sh to a SUPR + X shortcut to quickly toggle the daemon.
  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).
#!/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
  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).
Source Link
DATALOT
  • 459
  • 1
  • 5
  • 15

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
    bash $PROGRAM_PATH/daemon.sh &

    # 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 bash $HOME/.middle-paste/toggler.sh to a SUPR + X shortcut to quickly toggle the daemon.
  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).