How do I change the keyboard layout?

X11 provides a convenient utility called xmodmap to redefine keybindings. Keys are mapped as codes (keycodes) and symbols (keysyms). To see what your current key bindings are, type xmodmap -pke at the command line. The program dumpkeys also provides some useful information.

You can redefine mappings by typing xmodmap -e followed by the key assignments. These assignments be one of the following types:

 pointer = default              reset pointer buttons to default
 pointer = NUMBER ...           set pointer button codes
 keycode NUMBER = [KEYSYM ...]  map keycode to given keysyms
 keysym KEYSYM = [KEYSYM ...]   look up keysym and do a keycode
                                    operation
 clear MODIFIER                 remove all keys for this modifier
 add MODIFIER = KEYSYM ...      add the keysyms to the modifier
 remove MODIFIER = KEYSYM ...   remove the keysyms from the modifier
where NUMBER is a decimal, octal, or hex constant; KEYSYM is a valid Key Symbol name; and MODIFIER is one of the eight modifier names: Shift, Lock, Control, Mod1, Mod2, Mod3, Mod4, or Mod5. Lines beginning with an exclamation mark (!) are taken as comments. Case is significant except for MODIFIER names.

Note that Keysyms on the left hand side of the = sign are looked up before any changes are made; keysyms on the right are looked up after all of those on the left have been resolved. This makes it possible to swap modifier keys.

examples

             %  xmodmap -e "pointer = 3 2 1" 
This inverts the order of the buttons on a three-button mouse. This could be useful for left-handed users.
              %  xmodmap -e "keysym BackSpace = Delete"
This causes the backspace key to function as the delete key.

It is unlikely that you will want to explicity reconfigure your keyboard layout from the command line after every login. This reconfiguration can be done behind the scenes during startup by modifying your .xinitrc file contained in your HOME directory. The easiest way to do this is to add the line

	xmodmap - < .my_keyboard_defs 
where .my_keyboard_defs is a file containing keyboard mappings.

example definition file (courtesy of Tom Fine)

!.my_keyboard_defs - makes a DECStation keyboard act like a SUN

!fix the escape key first
keycode 191 = Escape

!move the backtick and the tilde to Sun kbd location
keycode 247 = grave asciitilde

!move the backslash and pipe to the wyse 50 location 
!(Dec right alt key)
keycode 178 = backslash bar

!make the delete key a backspace, make shift-delete be delete
keycode 188 = BackSpace Delete

!Deal with shift, control, and caps lock
remove Lock = Caps_Lock
remove Shift = Shift_L
remove Mod1 = Multi_key

!set caps lock key to be another control key
keysym Caps_Lock = Control_R

!set the left compose key to be caps lock
keycode 177 = Caps_Lock

!set the <> key to be a shift key.  This doesn't work, since
! only the first two shift keys are used, but it does disable
! the <> key.
keycode 201 = Shift_L

!apply the changes to the shift, control, caps lock
add Lock = Caps_Lock
add Control = Control_R
add Shift = Shift_L
add Mod1 = Multi_key
Modifiers such as Lock, Shift, and Control require some special handling as seen in the previous example. Consider swapping the Control and Shift Lock keys:
 ! Swap Caps_Lock and Control_L
 !
 remove Lock = Caps_Lock
 remove Control = Control_L
 keysym Control_L = Caps_Lock
 keysym Caps_Lock = Control_L
 add Lock = Caps_Lock
 add Control = Control_L
The keysyms must be removed from the modifier maps, the reassignments made, and the modified keysyms again added to the modifier maps.

Last Update: 1/10/94 JGW