Computer >> Computer tutorials >  >> System >> Linux

Rebind the Middle Mouse Click in Ubuntu: Step‑by‑Step Guide

Rebind the Middle Mouse Click in Ubuntu: Step‑by‑Step Guide

Rebind the Middle Mouse Click in Ubuntu: Step‑by‑Step Guide

Published May 27, 2023, 2:46 PM EDT

Hannah's love for Linux first blossomed while competing in CyberPatriot. She has studied disciplines like programming and computer repair from a young age, leading her to understand the value of good documentation — and how some documentation just isn't helpful to those without experience. That experience led her to strive to create straightforward, useful documentation. She currently studies computer science, focusing on subdisciplines like programming and Linux, at Eastern New Mexico University.

Hannah has written for MakeUseOf since April 2023, focusing on verticals like Linux, Programming, DIY, and Buyer's Guides. Beyond contributing to local publications, such as ENMU's The Chase newspaper, Hannah finds her passion in delivering relevant, recent, and accurate news to New Mexico locals on KENW News 3.

Sign in to your MakeUseOf account

Pasting with the middle mouse button is a convenient feature for many Ubuntu users, but you’re not alone if you worry about accidentally pasting private information with an unintentional click. Luckily, rebinding the middle mouse button in Ubuntu is a simple process.

How to Rebind Middle Mouse Click in Ubuntu

Rebinding your middle mouse button involves creating a configuration file. Before you create this file, you’ll need to determine the value associated with your middle mouse click and the keys or buttons you intend to rebind to. Start by entering the following command in the terminal:

sudo evtest

A list of all the devices connected to your computer will print. There will also be an active prompt that requests you to select a device event number. Find your mouse in the list and enter the event number.

Lines of text describing events from your mouse will print. Keep your mouse still and click the middle mouse button. Once the click event prints, copy the value associated with the middle mouse button—in this case, 90003.

Rebind the Middle Mouse Click in Ubuntu: Step‑by‑Step Guide

Finally, press Ctrl + C to end the event monitoring. Enter the evtest command again and find your keyboard in the list. Enter the associated event number and enter the key that you would like to bind the middle mouse button to.

The terminal will print two events each time you press and release a key. Write down the name associated with the keys you choose—in this case, KEY_BACKSPACE. Exit with Ctrl + C when you’re finished gathering key names.

Rebind the Middle Mouse Click in Ubuntu: Step‑by‑Step Guide

Next, you’ll need to write down the bus type, product number, vendor number, and version number associated with your mouse. Enter the following command (replacing N with the event number associated with your mouse) and write down the four codes that print:

grep "" /sys/class/input/eventN/id/*

Once you have the four numbers written down, you’re ready to create the configuration file. Enter the following command to create a new configuration file and open it in a text editor like nano or Vim:

sudo nano /etc/udev/hwdb.d/mouse-remap.hwdb

You will use the mouse identification numbers, the middle mouse button event code, and the key names to write your new configuration file. Paste the following code into the editor and fill in the unique information associated with your devices to complete your new configuration:

evdev:input:b[bustype]v[vendor]p[product]e[version]*
 ID_INPUT_KEY=1
 KEYBOARD_KEY_[scancode]=[keycode]

It's important to note that you can also map your middle mouse button to another mouse button. For example, you would enter btn_left as the keycode to map the middle mouse button to the left mouse button. The prefix, btn_, is required.

Unfortunately, it doesn't appear to be possible to map your mouse button to a multi-key combination.

The configuration file will look like this when you have finished writing. This computer has been configured to use the middle mouse button to Backspace. Make sure to write the keycode in all lowercase letters.

Rebind the Middle Mouse Click in Ubuntu: Step‑by‑Step Guide

Finally, you will need to allow your system to apply the new configuration. Enter the following command and then disconnect and reconnect your mouse from the computer:

sudo systemd-hwdb update

Test the middle mouse button to verify that the new configuration works. You may need to restart your computer to make the new configuration take effect.

How to Disable Middle Mouse Click

The idea of disabling the middle mouse click instead of remapping it might seem appealing if you're prone to accidentally clicking it. Fortunately, it's possible to disable the middle mouse click by remapping it properly.

Follow the process outlined above to learn about your middle mouse button input event value and the identification numbers associated with your mouse. Create the HWDB file in the same way as outlined. You will use reserved as the keycode to disable the middle mouse click.

If your system uses the X11 display server, you also have the option of disabling the middle mouse click through xinput. You can start the process of disabling the middle mouse button by finding the xinput identifier for your mouse with the following command:

xinput list

Write down the ID number for every mouse pointer listed in the terminal. There may be more than one. Next, you will need to remap the input for each mouse button. Enter the following command using the ID number for all mouse pointers, replacing N and M with the IDs.

xinput set-button-map N 1 0 3
xinput set-button-map M 1 0 3

The mouse buttons are typically mapped as "1 2 3," referring to action 1 (left click), action 2 (middle click), and action 3 (right click). You can disable or remap the mouse buttons in any way you prefer by changing the xinput mapping number.

Improve Your Workflow by Remapping Mouse Buttons

You don’t have to stop at just remapping your middle mouse button. If you're ready to streamline your work process, it's better to consider purchasing an incredible new drag-clicking multi-button mouse.