Xmodmap: Remap any key using xmodmap

Xmodmap is a handy tool to remap any key on your keyboard especially Control, Escape, and, Capslock. These keys are either placed in an unwanted position causing pain in the hand or not being used at all.

Table of contents

Installation of xmodmap

Debian

apt-get install x11-xserver-utils

Ubuntu

apt-get install x11-xserver-utils

Arch Linux

pacman -S xorg-xmodmap

Kali Linux

apt-get install x11-xserver-utils

CentOS

yum install xorg-xmodmap

Fedora

dnf install xorg-xmodmap

Raspbian

apt-get install x11-xserver-utils

Configuration files and auto-start at system startup

The file ~/.xmodmap is automatically sourced and executed by Display Managers such as GDM, XDM, and LightDM. For file positions other than that such as ~/.config/X11/xmodmap, you need to put the following command in your file ~/.xprofile:

xmodmap ~/.config/X11/xmodmap

To uncomment lines in this file, prepend them with an exclamation mark (!).

Modifier keys

The modifier keys are Shift, Super, CapsLock, etc. The list can be obtained using command xmodmap -pm.

xmodmap printing modifier keys
Figure: xmodmap printing modifier keys

If you need to interfere with modifier keys, use clear and add command as given below:

clear lock
keycode 9 = Caps_Lock 
keycode 66 = Escape
add lock = Caps_Lock

The above example interchanges Caps_Lock with Escape. The keycodes (which are the numbers in the above output – 9 and 66) and keysyms (which in the above examples are Caps_Lock and Escape) can be obtained using xmodmap -pke:

[ajay@lenovo ~]$ xmodmap -pke
…
keycode  24 = q Q q Q
keycode  25 = w W w W
keycode  26 = e E e E
keycode  27 = r R r R
…

The above output looks more like a table. The second column after the equal (=) sign gets activated when we press the key Shift with the given keys. The keycodes are unique to our keys and they cannot be changed.

Note: Your output of the commands xmodmap -pke and xmodmap -pm might be slightly different than what is shown above depending upon your keyboard.

Understanding using examples

Example 1: Interchange BackSpace and semicolon

The Xmodmap output when there is no remapping:

[ajay@lenovo ~]$ xmodmap -pke | grep -Ei 'backspace|semicolon'
keycode  22 = BackSpace BackSpace BackSpace BackSpace
keycode  47 = semicolon colon semicolon colon

To interchange these keys, put the following two lines in your ~/.xmodmap file:

keycode 47 = BackSpace colon semicolon colon
keycode 22 = semicolon semicolon semicolon semicolon

Example 2: Interchange Delete and apostrophe

The Xmodmap output when there is no remapping:

[ajay@lenovo ~]$ xmodmap -pke | grep -Ei 'delete|apostrophe'
keycode  48 = apostrophe quotedbl apostrophe quotedbl
keycode  91 = KP_Delete KP_Decimal KP_Delete KP_Decimal
keycode 119 = Delete NoSymbol Delete

Use the following mapping for your file ~/.xmodmap:

keycode  48 = Delete quotedbl apostrophe quotedbl
keycode 119 = apostrophe NoSymbol Delete

Example 3: Interchange slash and up

The Xmodmap output when there is no remapping:

[ajay@lenovo ~]$ xmodmap -pke | grep -Ei 'slash|up'
keycode  51 = backslash bar backslash bar
keycode  61 = slash question slash question
keycode  66 = Super_L NoSymbol Super_L
keycode  80 = KP_Up KP_8 KP_Up KP_8
keycode 111 = Up NoSymbol Up
keycode 133 = Super_L NoSymbol Super_L
keycode 134 = Super_R NoSymbol Super_R
keycode 151 = XF86WakeUp NoSymbol XF86WakeUp
keycode 185 = XF86ScrollUp NoSymbol XF86ScrollUp
keycode 206 = NoSymbol Super_L NoSymbol Super_L
keycode 233 = XF86MonBrightnessUp NoSymbol XF86MonBrightnessUp
keycode 238 = XF86KbdBrightnessUp NoSymbol XF86KbdBrightnessUp

Note: Instead of grep, you can also use fzf which is more convenient.

~/.xmodmap mapping:

keycode 61 = Up NoSymbol Up
keycode 111 = slash question slash question

Example 4: Interchange Left and Alt_R

Before the remapping,

keycode 108 = Alt_R Meta_R Alt_R Meta_R
keycode 113 = Left NoSymbol Left 

Use the following in your file ~/.xmodmap:

clear mod1
keycode 108 = Left NoSymbol Left 
keycode 113 = Alt_R Meta_R Alt_R Meta_R
add mod1 = Alt_L Alt_R Meta_L

Here, When I interchange only the first column, pressing 108 key for longer time does not work. So, I am interchanging everything between 108 and 113.

Example 5: Interchange Super_R and Down

clear mod4
keycode 134 = Down NoSymbol Down
keycode 116 = Super_R NoSymbol Super_R 
add mod4 = Super_L Super_R Super_L Hyper_L

When I interchange only the first column, pressing the 134 key for a longer time does not work. So, I am interchanging everything between 134 and 116.

Example 6: Interchange Right and Menu

keycode 135 = Right NoSymbol Menu
keycode 114 = Menu NoSymbol Right

Here, I am swapping only the first column.

Example 7: Interchange AltGr and Menu (useful for Thinkpad laptops)

Thinkpad laptops do not have a Menu key, so use the following lines:

clear mod1
keycode 108 = Menu NoSymbol Menu
add mod1 = Alt_L Alt_L Meta_L

Example 8: Interchange Control_R and Super_R

clear control
clear mod4
keycode 105 = Super_R NoSymbol Super_R 
add control = Control_L Control_R
add mod4 = Super_L Super_R Super_L Hyper_L

Conclusion

There is another similar program called Xcape. It allows you to use a modifier key as another key when pressed and released on its own. I recommend you to use Xmodmap with this. I currently have mapped Capslock with Super key (Mod4) when it is pressed with some other key and as Escape when it is pressed alone. Look at my article on xcape over here to learn more.

Leave a Comment

Your email address will not be published. Required fields are marked *