Changing Linux keyboard layout in GUI and CLI
I am proud owner of a Thinkpad T520. It has many design choices that just make it work for me. However, it has a typing ergonomy problem: the large distance of the Home
and End
keys from the arrow keys on the keyboard. Writing a lot of text and code, I heavily rely on all these keys to navigate, and I find having to move my hand a lot between these keys disturbing. Thankfully, this issue is easy to fix through remapping the Internet navigation buttons placed right above the arrow keys.
Keycodes for GUI
To find out the keycode for remapping an arbitrary key in the GUI, start xev
in a command line, then press the key to be remapped and note its keycode in the output. For my Back
button, this is 166
:
KeyPress event, serial 40, synthetic NO, window 0x5e00001,
root 0xbe, subw 0x0, time 2410131, (-57,523), root:(529,552),
state 0x0, keycode 166 (keysym 0xff50, Home), same_screen YES,
XKeysymToKeycode returns keycode: 110
XLookupString gives 0 bytes:
XmbLookupString gives 0 bytes:
XFilterEvent returns: False
Remapping for GUI
Edit file /usr/share/X11/xkb/symbols/gb
(where gb
depends on the local keyboard layout you actually use), and add these in a convenient location (I chose inside xkb_symbols "basic"
):
// David: remap Back and Forward buttons
key <I166> { [ Home ] };
key <I167> { [ End ] };
To test the effects immediately, use setxkbmap gb
.
Keycodes for CLI
Use showkey --scancodes
to obtain the scancode. showkey
exits if no key is pressed for 10 seconds. For my Back
button, the scancode is 158
.
Remapping for CLI
Edit file /usr/share/kbd/keymaps/i386/qwerty/uk.map.gz
(unzip, edit, and re-zip):
# David: remap Back and Forward buttons in CLI
keycode 158 = Home
keycode 159 = End
To test the effects immediately, use loadkeys uk
. Again, uk
is specific to my layout, and you need to know which one you are actually using.