I am left handed person.
Therefore I want to interchange the typical mouse buttons for right and left clicks.

I want to do this via terminal. May someone please suggest the command for it?

Is it possible to interchange these clicks at the login screen also? I am using Ubuntu 12.04.


7 Answers 7

xmodmap -e "pointer = 3 2 1"

in a terminal should work. If you want to run that command at the start of lightdm (the default login screen), you can edit /etc/lightdm/lightdm.conf like described here - use the xmodmap command instead of xbacklight in the script, of course.

  • 2
    this works for the present session.. how to make it permanent???
    – Fookraa
    Jun 16, 2012 at 18:33
  • 3
    It should be permanent if you put it in /etc/lightdm/lightdm.conf. If not, you can put it in Startup Applications or create a .desktop file in ~/.config/autostart from the terminal.
    – elmicha
    Jun 16, 2012 at 19:45
  • I have no /etc/lightdm/lightdm.conf file. Is there somewhere template for it? Aug 19, 2019 at 15:21
  • This answer should contain a warning that xmodmap is global i.e. if you have a different pointing device like a graphic tablet or an air mouse, it will also be affected.
    – cprn
    Dec 4, 2022 at 17:43

You can use also xinput to do this.

It will switch the buttons on one mouse only.

First use-

xinput list

To see where you mouse is will be somthing like

"USB Mouse"     id=12       [slave pointer]

So use the id number for the next command to switch the button assignment.

xinput set-button-map 12 3 2 1

So, you would switch out 12 with the id number of your mouse.

Source: 1

  • 2
    I find this useful because I often have two mice, left and right. This technique allows them to be set to be the mirror image of each other.
    – Rick-777
    Jun 8, 2015 at 9:13
  • And then you can make this permanent by creating a Xorg device configuration as explained here: linus.haxx.se/2013/03/07/mouse-button-mapping-in-xorg-conf
    – Apteryx
    Nov 19, 2015 at 19:11
  • 2
    In case my pointer devices change IDs, I look up the ID as part of the command line: xinput set-button-map $(xinput list --id-only "Kensington Kensington Expert Mouse") 3 2 1 The string in quotes comes from "xinput list" above.
    – akom
    Apr 27, 2017 at 20:04
  • 1
    This should be marked as answer rather than using xmodmap
    – ptgamr
    Feb 11, 2018 at 21:41
  • 1
    You can use device name instead of device Id, like xinput get-button-map 'USB Optical Mouse'. Mar 27, 2019 at 18:54

xmodmap is the command to change the mouse button mapping:

xmodmap -e "pointer = 3 2 1"

will remap the first three buttoons, left, middle and right, to act like , right, middle and left - not actually changing the middle button mapping.

That applies the change to the current session - more precisely, to the current X display, for exanple :0.

To save the change persistent over sessions, put the mapping into the file


If it does not exist, create it, liketouch ~/.Xmodmap

Edit it to add the line

pointer = 3 2 1

If there already is a pointer line, it may have some function, of course; Instead of replacing it, better modify the first three values - keeping a comment with the old:

For example,

pointer = 1 2 3 4 5 16 17 8 9 10 11 12 13 14 15 6 7 18 19 20 21 22 23 24


!pointer = 1 2 3 4 5 16 17 8 9 10 11 12 13 14 15 6 7 18 19 20 21 22 23 24
pointer = 3 2 1 4 5 16 17 8 9 10 11 12 13 14 15 6 7 18 19 20 21 22 23 24

I'm used to ~/.Xmodmap allways just working; But see .Xmodmap works only when automatic login disabled? if it does not.



A full solution would look something like this:

device_id=$(xinput list | grep "Vert.*Mouse" | cut -f2 | cut -c4-)
xinput set-button-map $device_id 1 2 3 5 4 6 7 8

Full Answer

Using xmodmap affects all pointer devices. To make that change per device name you need to use xinput (as described by Mateo) but the device ID on xinput list might change for number of reasons so to make it work correctly each time you need to dynamically get the correct device ID on startup.

For example:

# find your device ID on the list
xinput list

# check the current buttons map:
xinput get-button-map $your_device_id

# find the problematic buttons
# press ctrl+c when done
xinput --test $your_device_id

My device name is Logitech MX Vertical Ergonomic Mouse and I need to switch places on buttons 4 and 5 in a following button map:

1 2 3 4 5 6 7 8

So to get the device ID reliably I use:

xinput list | grep "Vert.*Mouse" | cut -f2 | cut -c4-
  • xinput list - spits out device names and IDs
  • grep "Vert.*Mouse" - finds the line containing Vert and Mouse
  • cut -f2 - cuts second "field" from that line, i.e. id=17
  • cut -c4- - cuts from the 4th "character", i.e. 17

Now that I have the command that always spits out the correct device ID, I can put it in parentheses like so: $(command) - it runs the command in a subshell and replaces it with its output. So a full one-liner that can be placed in a starting script looks as mentioned in TL;DR.


Mateo's answer is right, but I wanted to present another way to set up this configuration with xinput.

It will also switch the buttons on one mouse only.

First list your devices:

xinput list

Your mouse will show up in this list somewhere:

⎡ Virtual core pointer                      id=2    [master pointer  (3)]
⎜   ...
⎜   ↳ USB Mouse                             id=11   [slave  pointer  (2)]
⎜   ...

Use the id number of your device for the next commands to switch the button assignment. Verify that your device has a setting to enable Left-Handed mode:

xinput list-props 11                            
Device 'USB Mouse':
    Device Enabled (155):   1
    libinput Left Handed Enabled (311): 0

As you can see, that is property number 311. It's an 8-bit number with values 0 or 1. Now we can enable/disable that for your device:

xinput set-int-prop 11 311 8 1

Execute this code in command prompt, it will remove and reset Optical mouse and touchpad to default- left click is for clicking and right click is for options.

modprobe -r psmouse
modprobe psmouse proto=imps

For Ubuntu ~12.04 and prior

#!/usr/local/bin/tcsh -fb

# switches between right and left mouse
set leftMouseHand = `gconftool-2 --get /desktop/gnome/peripherals/mouse/left_handed`
if ( "$leftMouseHand" == "false" ) then
    gconftool-2 --set /desktop/gnome/peripherals/mouse/left_handed true --type boolean
    gconftool-2 --set /desktop/gnome/peripherals/mouse/cursor_theme Oxygen_White_Big --type string
    gconftool-2 --set /desktop/gnome/peripherals/mouse/left_handed false --type boolean
    gconftool-2 --set /desktop/gnome/peripherals/mouse/cursor_theme Oxygen_White_Big --type string

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .