How to disable Middle Mouse Paste on Ubuntu automatically on startup

I'm trying to make a script run on startup, so that I don't have to do it manually every time, with a terminal window being open constantly.

This is the script: anti-midmouse-paste.sh

#!/bin/bash

while(true)
do
    echo -n | xsel -n -i
    sleep 0.5
done

It is a script I found online that clears the selected copied text. It works flawlessly when I run it regularly with: sh anti-midmouse-paste.sh

However, when I've inputted the script in /etc/systemd/system using nano, it doesn't seem to work on startup (despite enabling it with sudo systemctl enable anti-midmouse-paste), nor when i start the script using sudo systemctl start anti-midmouse-paste. I have done chmod +x on both the .service and the .sh file.

Here is the .service content:

[Unit]
Description=Stops middlemouse paste from working

[Service]
ExecStart=/usr/local/bin/anti-midmouse-paste.sh  #in this line specify the path to the script.
Type=simple
Restart=on-failure
Restartsec=10
KillMode=process

[Install]
WantedBy=multi-user.target

And when I check sudo systemctl status anti-midmouse-paste.service:

● anti-midmouse-paste.service - Stops middlemouse paste from working
         Loaded: loaded (/etc/systemd/system/anti-midmouse-paste.service; enabled; vendor preset: enabled)
         Active: active (running) since Fri 2021-10-15 19:59:44 CEST; 27min ago
       Main PID: 889 (anti-midmouse-p)
          Tasks: 2 (limit: 19018)
         Memory: 2.4M
         CGroup: /system.slice/anti-midmouse-paste.service
                 ├─  889 /bin/bash /usr/local/bin/anti-midmouse-paste.sh #in this line specify the path to the script.
                 └─18219 sleep 0.5
    
    Oct 15 20:27:01 User anti-midmouse-paste.sh[18202]: xsel: Can't open display: (null)
    Oct 15 20:27:01 User anti-midmouse-paste.sh[18202]: : Inappropriate ioctl for device
    Oct 15 20:27:01 User anti-midmouse-paste.sh[18206]: xsel: Can't open display: (null)
    Oct 15 20:27:01 User anti-midmouse-paste.sh[18206]: : Inappropriate ioctl for device
    Oct 15 20:27:02 User anti-midmouse-paste.sh[18210]: xsel: Can't open display: (null)
    Oct 15 20:27:02 User anti-midmouse-paste.sh[18210]: : Inappropriate ioctl for device
    Oct 15 20:27:02 User anti-midmouse-paste.sh[18214]: xsel: Can't open display: (null)
    Oct 15 20:27:02 User anti-midmouse-paste.sh[18214]: : Inappropriate ioctl for device
    Oct 15 20:27:03 User anti-midmouse-paste.sh[18218]: xsel: Can't open display: (null)
    Oct 15 20:27:03 User anti-midmouse-paste.sh[18218]: : Inappropriate ioctl for device

As you can see, it says the script loaded and that it is running. But the logs show something that I do not understand and have not managed to find anywhere. Apparently it has something to do with me using xsel.

Hopefully someone can make sense of this and help. Thanks in advance.


Solution 1:

This is a guide on how to disable Middle Mouse Paste on Ubuntu automatically on startup. It was previously a post asking for help, but now that I have learned how to do it, I will share how I did it.

This uses a script I found in another post that clears the clipboard for your middle-mouse button, so that it doesn't get pasted when you click the scroll wheel. Found here: https://askubuntu.com/a/4644/1481518. Credit goes to him.

Step 0: Install xsel (Tool for manipulation of the X selection): sudo apt-get install xsel. Once you have installed xsel, you can continue.

Step 1: Create the script - Open up your text editor of choice, and type in the following code:

#!/bin/bash

while(true)
do
    echo -n | xsel -n -i
    sleep 0.5
done

Save the file as anti-midmouse-paste.sh (the name can be something else but make sure to add the .sh).

Also make the script file executable: chmod +x /path/to/file/anti-midmouse-paste.sh

Step 2: Now that we have created the script, it's time to make it run whenever we boot up our computer.

Open up another window of your text editor, and paste in the following:

[Desktop Entry]
Type=Application
Name=Anti Midmouse Paste
Exec="/path/to/the/script/anti-midmouse-paste.sh" "--no-window"
X-GNOME-Autostart-enabled=true

Make sure you enter the correct path for the Exec=.

And save this file in ~/.config/autostart/ as anti-midmouse-paste.desktop (again, the name doesn't matter but make sure it ends with .desktop)

If you can't find .config it is because it's a "hidden" directory. To make it show, press Ctrl + H and all hidden directories and files will show.

Step 4: You're done.

Now whenever you boot up your computer, the script should run and you will no longer paste selected text with your middle-mouse button.

BONUS TIP:

The script clears your selection of text for apps such as Text Editor and Terminal.

What you can do to delay the deletion for selection of text (if you want to copy or erase text), is to change sleep 0.5 in your .sh file to a higher value. The number is in seconds.