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

Linux Keybinding How-to

  1. Keyboard Software Guide
  2. Swap Control Alt Keys
  3. Set F2 F3 F4 to Cut Copy Paste
  4. CapsLock → Escape
  5. CapsLock → Home
  6. Shift Lock
  7. Key to Switch App
  8. Key Repeat Rate
  9. System Keyboard Layout
  10. Switch Keyboard Layout
  11. Change Volume
  12. xmodmap
  13. xbindkeys
  14. xvkbd
  15. Find Keyboard Key Scancode
  16. X11 Keyboard Key Names
  17. 2 Layouts for 2 Keyboards
  18. Chinese Input Setup

Mouse

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

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