Linux: How to Swap Mouse Buttons

By Xah Lee. Date: . Last updated: .

Here's how to swap mouse buttons on Linux, by command line.

In terminal, type xinput --list to list devices.

linux xinput output 2015-11-08
Linux xinput output.

Each device has a name and id.

For example, i have “Logitech USB Trackball” with id 10.

You need the name or id to set its properties.

To swap mouse buttons, type this in terminal:

# swap the middle and right button for device 13
xinput --set-button-map 13 1 3 2
# swap the middle and right button for device named Logitech USB Trackball
xinput --set-button-map "Logitech USB Trackball" 3 2 1

Normally, the those button numbers in the command line are in order, like this: 1 2 3 ...

How to Find X11 Mouse Button Number?

Linux: X11 Mouse Button Numbering

Create Startup Script

The id for the input device may change on each time the machine starts, or if you plug/unplug your USB devices.

Better to use the device name. However, for some devices, the device name is listed twice.

~/ $ xinput --list
⎡ Virtual core pointer                          id=2    [master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer                id=4    [slave  pointer  (2)]
⎜   ↳ Logitech Gaming Mouse G600                id=8    [slave  pointer  (2)]
⎜   ↳ Logitech Gaming Mouse G600                id=9    [slave  pointer  (2)]
⎜   ↳ LVT Recon gaming mouse                    id=10   [slave  pointer  (2)]
⎜   ↳ LVT Recon gaming mouse                    id=11   [slave  pointer  (2)]
⎜   ↳ Logitech USB Trackball                    id=12   [slave  pointer  (2)]
⎜   ↳ Logitech G700 Laser Mouse                 id=15   [slave  pointer  (2)]
⎜   ↳ Logitech G700 Laser Mouse                 id=16   [slave  pointer  (2)]
⎣ Virtual core keyboard                         id=3    [master keyboard (2)]
    ↳ Virtual core XTEST keyboard               id=5    [slave  keyboard (3)]
    ↳ Power Button                              id=6    [slave  keyboard (3)]
    ↳ Power Button                              id=7    [slave  keyboard (3)]
    ↳ TrulyErgonomic.com Truly Ergonomic Computer Keyboard      id=13   [slave  keyboard (3)]
    ↳ TrulyErgonomic.com Truly Ergonomic Computer Keyboard      id=14   [slave  keyboard (3)]

You can have a shell script to grep the device name and get the id.

Here's sample scripts for my mouses.

#!/bin/sh

# swap middle and right button
CM_STORM_RECON_ID=$(xinput list | grep "Recon" | head -n 1 | sed -r 's/.*id=([0-9]+).*/\1/')
xinput --set-button-map ${CM_STORM_RECON_ID} 1 3 2

[see CM Storm Recon Mouse Review]

#!/bin/sh

# logitech g600
# swap middle and right buttons
G600_ID=$(xinput list | grep "G600" | head -n 1 | sed -r 's/.*id=([0-9]+).*/\1/')
# xinput --set-button-map ${G600_ID} 1 2 3
xinput --set-button-map ${G600_ID} 1 3 2

[see Logitech G600 Gaming Mouse]

Save any of the above in a file as ~/.xprofile.

The ~/.xprofile will run when X11 starts.

Why Swap Mouse Buttons?

Why Swap Mouse Right Button and Middle Button

See also: Windows: How to swap Mouse Buttons: Right-Click, Middle-Click .

Set Mouse Speed

Linux: How to Set Mouse Speed

If you have a question, put $5 at patreon and message me.

Linux Keybinding Howto

Mouse