{"id":2017,"date":"2022-05-04T20:46:22","date_gmt":"2022-05-04T15:16:22","guid":{"rendered":"https:\/\/smarttech101.com\/?p=2017"},"modified":"2023-09-10T08:39:38","modified_gmt":"2023-09-10T03:09:38","slug":"xmodmap-remap-any-key-using-xmodmap","status":"publish","type":"post","link":"https:\/\/smarttech101.com\/xmodmap-remap-any-key-using-xmodmap\/","title":{"rendered":"Xmodmap: Remap any key using xmodmap"},"content":{"rendered":"\n
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.<\/p>\n\n\n\n
Debian<\/p>\n\n\n\n
apt-get install x11-xserver-utils<\/code><\/pre>\n\n\n\nUbuntu<\/p>\n\n\n\n
apt-get install x11-xserver-utils<\/code><\/pre>\n\n\n\nArch Linux<\/p>\n\n\n\n
pacman -S xorg-xmodmap<\/code><\/pre>\n\n\n\nKali Linux<\/p>\n\n\n\n
apt-get install x11-xserver-utils<\/code><\/pre>\n\n\n\nCentOS<\/p>\n\n\n\n
yum install xorg-xmodmap<\/code><\/pre>\n\n\n\nFedora<\/p>\n\n\n\n
dnf install xorg-xmodmap<\/code><\/pre>\n\n\n\nRaspbian<\/p>\n\n\n\n
apt-get install x11-xserver-utils<\/code><\/pre>\n\n\n\nConfiguration files and auto-start at system startup<\/h2>\n\n\n\n
The file ~\/.xmodmap<\/code> 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<\/code>, you need to put the following command in your file ~\/.xprofile<\/code>:<\/p>\n\n\n\nxmodmap ~\/.config\/X11\/xmodmap<\/code><\/pre>\n\n\n\nTo uncomment lines in this file, prepend them with an exclamation mark (!<\/code>).<\/p>\n\n\n\nModifier keys<\/h2>\n\n\n\n
The modifier keys are Shift, Super, CapsLock, etc. The list can be obtained using command xmodmap -pm<\/code>.<\/p>\n\n\n\n
Figure: xmodmap printing modifier keys<\/figcaption><\/figure>\n\n\n\nIf you need to interfere with modifier keys, use clear<\/code> and add<\/code> command as given below:<\/p>\n\n\n\nclear lock\nkeycode 9 = Caps_Lock \nkeycode 66 = Escape\nadd lock = Caps_Lock<\/code><\/pre>\n\n\n\nThe above example interchanges Caps_Lock with Escape. The keycodes<\/code> (which are the numbers in the above output – 9 and 66) and keysyms<\/code> (which in the above examples are Caps_Lock<\/code> and Escape<\/code>) can be obtained using xmodmap -pke<\/code>:<\/p>\n\n\n\n[ajay@lenovo ~]$ xmodmap -pke\n\u2026\nkeycode 24 = q Q q Q\nkeycode 25 = w W w W\nkeycode 26 = e E e E\nkeycode 27 = r R r R\n\u2026<\/code><\/pre>\n\n\n\nThe above output looks more like a table. The second column after the equal (=<\/code>) 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.<\/p>\n\n\n\nNote<\/strong>: Your output of the commands xmodmap -pke<\/code> and xmodmap -pm<\/code> might be slightly different than what is shown above depending upon your keyboard.<\/p>\n\n\n\nUnderstanding using examples<\/h2>\n\n\n\nExample 1: Interchange BackSpace and semicolon<\/h3>\n\n\n\n
The Xmodmap output when there is no remapping:<\/p>\n\n\n\n
[ajay@lenovo ~]$ xmodmap -pke | grep -Ei 'backspace|semicolon'\nkeycode 22 = BackSpace BackSpace BackSpace BackSpace\nkeycode 47 = semicolon colon semicolon colon<\/code><\/pre>\n\n\n\nTo interchange these keys, put the following two lines in your ~\/.xmodmap<\/code> file:<\/p>\n\n\n\nkeycode 47 = BackSpace colon semicolon colon\nkeycode 22 = semicolon semicolon semicolon semicolon<\/code><\/pre>\n\n\n\nExample 2: Interchange Delete and apostrophe<\/h3>\n\n\n\n
The Xmodmap output when there is no remapping:<\/p>\n\n\n\n
[ajay@lenovo ~]$ xmodmap -pke | grep -Ei 'delete|apostrophe'\nkeycode 48 = apostrophe quotedbl apostrophe quotedbl\nkeycode 91 = KP_Delete KP_Decimal KP_Delete KP_Decimal\nkeycode 119 = Delete NoSymbol Delete<\/code><\/pre>\n\n\n\nUse the following mapping for your file ~\/.xmodmap<\/code>:<\/p>\n\n\n\nkeycode 48 = Delete quotedbl apostrophe quotedbl\nkeycode 119 = apostrophe NoSymbol Delete<\/code><\/pre>\n\n\n\nExample 3: Interchange slash and up<\/h3>\n\n\n\n
The Xmodmap output when there is no remapping:<\/p>\n\n\n\n
[ajay@lenovo ~]$ xmodmap -pke | grep -Ei 'slash|up'\nkeycode 51 = backslash bar backslash bar\nkeycode 61 = slash question slash question\nkeycode 66 = Super_L NoSymbol Super_L\nkeycode 80 = KP_Up KP_8 KP_Up KP_8\nkeycode 111 = Up NoSymbol Up\nkeycode 133 = Super_L NoSymbol Super_L\nkeycode 134 = Super_R NoSymbol Super_R\nkeycode 151 = XF86WakeUp NoSymbol XF86WakeUp\nkeycode 185 = XF86ScrollUp NoSymbol XF86ScrollUp\nkeycode 206 = NoSymbol Super_L NoSymbol Super_L\nkeycode 233 = XF86MonBrightnessUp NoSymbol XF86MonBrightnessUp\nkeycode 238 = XF86KbdBrightnessUp NoSymbol XF86KbdBrightnessUp<\/code><\/pre>\n\n\n\n