Skip to content

Sixian Li

Ubuntu 18.04 Remap mouse buttons



  1. Write a script to remap mouse buttons
  2. Run the script when the wireless mouse is connected

This is my mouse:


The default right-click is the bottom right button, but I would like to use the top right button instead.

Remap mouse buttons

First, xinput gives the id and name assigned to my mouse. Then, xinput list 18(device id) gives information about all buttons on my mouse.

However, the labels are quite confusing. To find out which button is pressed, run xinput test 18(device id) and press buttons you want to switch.

This is much clearer than the “Button Side”. To remap buttons, simply use xinput set-button-map $mouse_id 1 2 8 4 5 6 7 3 9 which switches 3 and 8.

Problem: When I restart the laptop, everything goes back to the default settings. With the help of StackOverflow, I created a script called

I added this to the Start Up Applications. Unfortunately, this didn’t work because if I didn’t connect the mouse when the computer started, the system wouldn’t find the device let alone remap buttons.

NOTE: I highly recommend you keep track of the error message by changing the second line to:

Additional resources:

  1. Details on 2>&1

  2. How to avoid “Permission denied” when redirecting

Run the script when the mouse is connected

What I should do is executing the script when the mouse is connected via Bluetooth. udev rules are perfect for this job.

This tutorial on how to write basic udev rules helped me a lot.

Before writing rules, we need to gather information about the device. Run udevadm monitor and reconnect the device to get its path. Then, run udevadm info to get detailed information.

Now we have enough info to write the 88-remapmouse.rules:

== match the value

= assign a value (overwrite)

+= add another value

The script seems daunting, but what it says is simply “When the device with matching values is detected(connected), run /home/lisixian/ (the script in the first section)“.

NOTE: ENV{DISPLAY}=":0.0" and ENV{XAUTHORITY}="/run/user/1000/gdm/Xauthority are important. From the tutorial:

Those variables are essential when interacting with the X server programmatically, to setup some needed information: with the DISPLAY variable, we specify on what machine the server is running, what display and what screen we are referencing, and with XAUTHORITY we provide the path to the file which contains Xorg authentication and authorization information.

I got Cannot connect to X server error without these two lines.


Now, disconnect your device and connect again to see the results.