Linux: How to Swap Mouse Buttons

By Xah Lee. Date: . Last updated: .

This page shows you how to swap mouse buttons on Linux, by command line. For example:

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

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

Each device will have a name and id.

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

You need the name or id to set its properties.

To swap in Ubuntu Linux, type this in terminal:

# find out the device id
xinput --list

# 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 the X11 Mouse Button Number?

See: Linux: X11 Mouse Button Numbering.

Create Startup Script

The id for the input device may change on each start up of machine, if you plug/unplug your USB devices.

Better to use the device name. However, for some devices, the device name is listed twice. So, you can have a shell line to grep the name and get the id.

~/ $ 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)]

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 Review

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

The ~/.xprofile will be read when X11 starts.

Why Swap Mouse Buttons?

See: Why Swap Mouse Middle-Click and Right-Click?

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

Linux: How to Set Mouse Speed

Linux: How to Set Mouse Speed

Mouse topic

  1. Linux: Mouse Hover to Auto-Raise Window
  2. Linux: Mouse Hover-Click
  3. Linux: Set Mouse Single-Click to Open File
  4. Linux: How to Set Mouse Speed
  5. Linux: How to Swap Mouse Buttons
  6. Linux: Set Trackball/Mouse Do Scroll
  7. Linux: Setup Accelerated Scroll and AutoScroll
  8. Linux: X11 Mouse Button Numbering