Difference between revisions of "Xmodmap"

From ArchWiki
Jump to: navigation, search
m (fixed category)
 
(71 intermediate revisions by 22 users not shown)
Line 1: Line 1:
 +
{{Lowercase title}}
 
[[Category:Keyboards]]
 
[[Category:Keyboards]]
[[Category:X Server]]
+
[[Category:X server]]
 
[[de:Xmodmap]]
 
[[de:Xmodmap]]
 
[[fr:Xmodmap]]
 
[[fr:Xmodmap]]
[[zh-CN :Xmodmap]]
+
[[ja:Xmodmap]]
{{Article summary start}}
+
[[ru:Xmodmap]]
{{Article summary text|A general overview of modifying keymaps and pointer mappings with xmodmap.}}
+
[[zh-hans:Xmodmap]]
{{Article summary heading|Related}}
+
{{Related articles start}}
{{Article summary wiki|Xorg}}
+
{{Related|Xorg}}
{{Article summary wiki|Extra Keyboard Keys}}
+
{{Related|Extra keyboard keys}}
{{Article summary wiki|Extra Keyboard Keys in Xorg}}
+
{{Related|Extra keyboard keys in Xorg}}
{{Article summary wiki|Extra Keyboard Keys in Console}}
+
{{Related|Extra keyboard keys in console}}
{{Article summary end}}
+
{{Related|Xbindkeys}}
 +
{{Related articles end}}
  
'''Xmodmap''' is a utility for modifying keymaps and pointer button mappings in [[Xorg]].
+
''xmodmap'' is a utility for modifying keymaps and pointer button mappings in [[Xorg]].
 +
 
 +
''xmodmap'' is not directly related to [[X KeyBoard extension]] (XKB), as it uses different (pre-XKB) ideas on how ''keycodes'' are processed within X. Generally, it is only recommended for the simplest tasks. See [[X KeyBoard extension]] for advanced layout configuration.
 +
 
 +
{{Note|''xmodmap'' settings are reset by ''setxkbmap'', which not only alters the alphanumeric keys to the values given in the map, but also resets all other keys to the startup default. [http://wiki.linuxquestions.org/wiki/Configuring_keyboards]}}
  
 
== Introduction ==  
 
== Introduction ==  
The Linux kernel generates a code each time a key is pressed on a keyboard. That code is compared to a {{ic|table of keycodes}} defining a figure that is then displayed.
 
  
This process is complicated by [[Xorg]], which starts its own table of keycodes. Each keycode can belong to a {{ic|keysym}}. A keysym is like a function, started by typing a key. Xmodmap allows you to edit these keycode-keysym relations.
+
There are two types of keyboard values in [[Xorg]]: ''keycodes'' and ''keysyms''.
 +
 
 +
; keycode
 +
: The ''keycode'' is the numeric representation received by the kernel when a key or a mouse button is pressed.
 +
; keysym
 +
: The ''keysym'' is the value assigned to the ''keycode''. For example, pressing {{ic|A}} generates the {{ic|keycode 73}}, which is mapped to the {{ic|keysym 0×61}}, which matches {{ic|A}} in the [[Wikipedia:ASCII|ASCII table]].
 +
: The ''keysyms'' are managed by [[Xorg]] in a table of ''keycodes'' defining the ''keycode''-''keysym'' relations, which is called the [[#Keymap table|keymap table]]. This can be shown by running {{ic|xmodmap}}.
  
{{pkg|xkeycaps}} provides a graphical front-end to xmodmap, you can [[pacman|install]] it from the [[official repositories]].
+
== Installation ==
 +
 
 +
''xmodmap'' can be [[pacman|installed]] through the {{Pkg|xorg-xmodmap}} package from the [[official repositories]].
 +
 
 +
Optionally, install {{Pkg|xkeycaps}}, which is a graphical front-end to ''xmodmap''.
  
 
== Keymap table ==
 
== Keymap table ==
 +
 
Print the current keymap table formatted into expressions:
 
Print the current keymap table formatted into expressions:
{{hc|$ xmodmap -pke|2=keycode  57 = n N}}
 
  
Each keymap is followed by the {{Ic|keysyms}} it is mapped to. The above example indicates that the keycode {{ic|57}} is mapped to the lowercase ''n'', while the uppercase ''N'' is mapped to keycode {{Ic|57}} and {{Ic|Shift}}.
+
{{hc|$ xmodmap -pke|2=
 +
[...]
 +
keycode 57 = n N
 +
[...]
 +
}}
  
Each keysym column in the table corresponds to a particular key combination:
+
Each ''keycode'' is followed by the ''keysym'' it is mapped to. The above example indicates that the ''keycode'' {{ic|57}} is mapped to the lowercase {{ic|n}}, while the uppercase {{ic|N}} is mapped to ''keycode'' {{ic|57}} plus {{ic|Shift}}.
# {{Keypress|Key}}
 
# {{Keypress|Shift+Key}}
 
# {{Keypress|mode_switch+Key}}
 
# {{Keypress|mode_switch+Shift+Key}}
 
# {{Keypress|AltGr+Key}}
 
# {{Keypress|AltGr+Shift+Key}}
 
  
Not all keysyms have to be set, but if you want to assign a latter keysym without assigning earlier ones set them to {{ic|NoSymbol}}.
+
Each ''keysym'' column in the table corresponds to a particular combination of modifier keys:
 +
# {{ic|Key}}
 +
# {{ic|Shift+Key}}
 +
# {{ic|mode_switch+Key}}
 +
# {{ic|mode_switch+Shift+Key}}
 +
# {{ic|AltGr+Key}}
 +
# {{ic|AltGr+Shift+Key}}
  
You can check which keymap corresponds to a key on your keyboard with [[Extra Keyboard Keys#Using xev|xev]].
+
Not all ''keysyms'' have to be set, but to assign only a latter ''keysym'', use the {{ic|NoSymbol}} value.
  
{{tip|There are predefined descriptive keycodes that make mapping additional keys easier (e.g. {{ic|XF86AudioMute}}, {{ic|XF86Mail}}). Those keycodes can be found in: {{ic|/usr/include/X11/XF86keysym.h}}}}
+
To see which ''keycode'' corresponds to a key, see [[Extra keyboard keys#In Xorg]] for details on the ''xev'' utility which will output relevant keycode/keysym information about a key when you press it.
 +
 
 +
{{Tip|There are predefined descriptive ''keysyms'' for multimedia keys, e.g. {{ic|XF86AudioMute}} or {{ic|XF86Mail}}. These ''keysyms'' can be found in {{ic|/usr/include/X11/XF86keysym.h}}. Many multimedia programs are designed to work with these ''keysyms'' out-of-the-box, without the need to configure any third-party application.
 +
}}
 +
 
 +
Note that xmodmap is influenced by xkbd settings, so all eight keysym are available for the us(intl) xkbd layout but not for the default us (it is missing the ralt_switch symbol defined in level3). To have all 8 keysyms available you should configure the ''(intl)'' variant of the keyboard from xorg.conf or add, using us layout as an example, {{ic|setxkbmap -layout 'us(intl)'}} before calling xmodmap.
  
 
== Custom table ==
 
== Custom table ==
You can create your own map and store it in your home directory (i.e. {{ic|~/.Xmodmap}}). Print the current keymap table into a configuration file:
 
xmodmap -pke > ~/.Xmodmap
 
  
Make the desired changes to {{ic|~/.Xmodmap}} and then test the new configuration with:
+
To create a key map (i.e. {{ic|~/.Xmodmap}}):
  xmodmap ~/.Xmodmap
+
$ xmodmap -pke > ~/.Xmodmap
 +
 
 +
To test the changes:
 +
  $ xmodmap ~/.Xmodmap
 +
 
 +
=== Activating the custom table ===
  
{{Accuracy|Some desktop environments such as [[GNOME]] should automatically detect the file and ask you if you want to use it.}}
+
With [[GDM]], [[XDM]], [[KDM]] or [[LightDM]] there is no need to source {{ic|~/.Xmodmap}}. For [[startx]], use:
  
To activate your custom table when starting Xorg add the following:
+
{{hc|~/.xinitrc|<nowiki>
{{hc|~/.xinitrc|
+
[[ -f ~/.Xmodmap ]] && xmodmap ~/.Xmodmap
if [ -s ~/.Xmodmap ]; then
+
</nowiki>}}
    xmodmap ~/.Xmodmap
 
fi}}
 
  
 
Alternatively, edit the global startup script {{ic|/etc/X11/xinit/xinitrc}}.
 
Alternatively, edit the global startup script {{ic|/etc/X11/xinit/xinitrc}}.
  
 
=== Test changes ===
 
=== Test changes ===
You can also make temporary changes for the current session. For example:
 
xmodmap -e "keycode  46 = l L l L lstroke Lstroke lstroke"
 
xmodmap -e "keysym a = e E"
 
  
== Special keys/signals ==
+
To make temporary changes:
 +
$ xmodmap -e "keycode  46 = l L l L lstroke Lstroke lstroke"
 +
$ xmodmap -e "keysym a = e E"
 +
 
 +
== Modifier keys ==
 +
 
 +
''xmodmap'' can also be used to override [[Wikipedia:Modifier key|modifier keys]], e.g. to swap {{ic|Control}} and {{ic|Super}} (the [[Wikipedia:Windows key|Windows keys]]).
 +
 
 +
Before assignment the modifier keys need to be empty. {{ic|!}} is a comment, so only the modifiers {{ic|Control}} and {{ic|Mod4}} get cleared in the following example. Then the ''keysyms'' {{ic|Control_L}}, {{ic|Control_R}}, {{ic|Super_L}} and {{ic|Super_R}} are assigned to the opposite modifier. Assigning both left and right to the same modifier means that both keys are treated the same way.
 +
 
 +
{{hc|~/.Xmodmap|2=
 +
[...]
 +
!clear Shift
 +
!clear Lock
 +
clear Control
 +
!clear Mod1
 +
!clear Mod2
 +
!clear Mod3
 +
clear Mod4
 +
!clear Mod5
 +
!add Shift  = Shift_L Shift_R
 +
!add Lock    = Caps_Lock
 +
add Control = Super_L Super_R
 +
!add Mod1    = Alt_L Alt_R
 +
!add Mod2    = Mode_switch
 +
!add Mod3    =
 +
add Mod4    = Control_L Control_R
 +
!add Mod5    =
 +
}}
 +
 
 +
{{Note|The example assumes that the {{ic|Control_L}} and {{ic|Control_R}} keysyms were assigned to the {{ic|Control}} modifier, and {{ic|Super_L}} and {{ic|Super_R}} keysyms to the {{ic|Mod4}} modifier. If you get the following error message {{ic|X Error of failed request:  BadValue (integer parameter out of range for operation)}}, you will need to adapt accordingly. Running {{ic|xmodmap}} produces a list of modifiers and keys that are assigned to them.}}
 +
 
 +
The following example modifies {{ic|CapsLock}} to {{ic|Control}}, and {{ic|Shift+CapsLock}} to {{ic|CapsLock}}:
 +
{{hc|~/.Xmodmap|2=
 +
clear lock
 +
clear control
 +
add control = Caps_Lock Control_L Control_R
 +
keycode 66 = Control_L Caps_Lock NoSymbol NoSymbol
 +
}}
 +
 
 +
== Reverse scrolling ==
 +
 
 +
{{Merge|Mouse buttons|xmodmap is not the only way to do this.}}
 +
 
 +
The [http://who-t.blogspot.com/2011/09/natural-scrolling-in-synaptics-driver.html natural scrolling] feature available in OS X Lion (mimicking smartphone or tablet scrolling) can be [https://bbs.archlinux.org/viewtopic.php?id=126258 replicated] with ''xmodmap''. Since the synaptics driver uses the buttons 4/5/6/7 for up/down/left/right scrolling, you simply need to swap the order of how the buttons are declared in {{ic|~/.Xmodmap}}:
 +
 
 +
{{hc|~/.Xmodmap|2=
 +
pointer = 1 2 3 '''5 4''' 7 6 8 9 10 11 12
 +
}}
 +
 
 +
Then update ''xmodmap'':
 +
$ xmodmap ~/.Xmodmap
 +
 
 +
== Swapping mouse buttons ==
 +
 
 +
{{Merge|Mouse buttons|xmodmap is not the only way to do this.}}
 +
 
 +
The left, middle and right mouse buttons correspond to buttons 1,2 and 3 respectively in the synaptics driver. To swap left and right mouse buttons, again simply reverse the order in which they are listed in your {{ic|~/.Xmodmap}}:
 +
 
 +
{{hc|~/.Xmodmap|2=
 +
pointer = '''3 2 1'''
 +
}}
 +
 
 +
This should suffice for a simple mouse setup. Again, update ''xmodmap'':
 +
$ xmodmap ~/.Xmodmap
  
You can also also edit the keys: {{Keypress|Shift}}, {{Keypress|Ctrl}}, {{Keypress|Alt}} and {{Keypress|Super}} (there always exists a left and a right one (Alt_R=AltGr)).
+
== Templates ==
  
For example this can be useful if your right Control key is not working like your left one but you would like it to.
+
=== Spanish ===
  
At first you have to delete/clear the signals that should be edited. In the beginning of your {{ic|~/.Xmodmap}}:
+
{{hc|~/.Xmodmap|
  !clear Shift
+
keycode 24 &#61; a A aacute Aacute ae AE ae
  !clear Lock
+
keycode 26 &#61; e E eacute Eacute EuroSign cent EuroSign
  clear Control
+
keycode 30 &#61; u U uacute Uacute downarrow uparrow downarrow
  !clear Mod1
+
keycode 31 &#61; i I iacute Iacute rightarrow idotless rightarrow
  !clear Mod2
+
keycode 32 &#61; o O oacute Oacute oslash Oslash oslash
  !clear Mod3
+
keycode 57 &#61; n N ntilde Ntilde n N n
  clear Mod4
+
keycode 58 &#61; comma question comma questiondown dead_acute dead_doubleacute dead_acute
  !clear Mod5
+
keycode 61 &#61; exclam section exclamdown section dead_belowdot dead_abovedot dead_belowdot
keycode   8 =
+
!Maps the Mode key to the Alt key
...
+
keycode 64 &#61; Mode_switch
Remember, {{ic|!}} is a comment so only {{Keypress|Control}} and {{Keypress|Mod4}} (Standard: Super_L Super_R) get cleared.
+
}}
  
Write the new signals at the end of {{ic|~/.Xmodmap}}
+
=== Turn CapsLock into Control ===
keycode 255 =
 
!add Shift  = Shift_L Shift_R
 
!add Lock    = Caps_Lock
 
add Control = Super_L Super_R
 
!add Mod1    = Alt_L Alt_R
 
!add Mod2    = Mode_switch
 
!add Mod3    =
 
add Mod4    = Control_L Control_R
 
!add Mod5    =
 
The {{Keypress|Super}} keys have now been exchanged with the {{Keypress|Ctrl}} keys.
 
  
If you get the following error message {{ic|X Error of failed request:  BadValue (integer parameter out of range for operation)}} it means the key you are trying to add is already in another modifier, so remove it using "remove MODIFIERNAME = KEYSYMNAME". Running {{ic|xmodmap}} gives you a list of modifiers and keys that are assigned to them.
+
Simplest example of changing {{ic|CapsLock}} into {{ic|Control}}.
  
== Reverse Scrolling ==
+
{{hc|~/.Xmodmap|<nowiki>
The natural scrolling feature available in OS X Lion (mimicking smartphone or tablet scrolling) can be replicated with xmodmap. Since the synaptics driver uses the buttons 4/5/6/7 for up/down/left/right scrolling, you simply need to swap the order of how the buttons are declared in {{ic|~/.Xmodmap}}.
+
clear lock
 +
clear control
 +
keycode 66 = Control_L
 +
add control = Control_L Control_R
 +
</nowiki>}}
  
Open {{ic|~/.Xmodmap}} and append the following line to the file:
+
=== Turn CapsLock into Control, and LeftControl into Hyper ===
pointer = 1 2 3 5 4 7 6 8 9 10 11 12
 
Note how the 4 and 5 have been reversed.
 
  
Then update xmodmap:
+
Laptop users may prefer having {{ic|CapsLock}} as {{ic|Control}}. The {{ic|Left Control}} key can be used as a {{ic|Hyper}} modifier (an additional modifier for emacs or openbox or i3).
xmodmap ~/.Xmodmap
 
  
To return to regular scrolling simply reverse the order of the 4 and 5 or delete the line altogether. For more information check Peter Hutterer's post, [http://who-t.blogspot.com/2011/09/natural-scrolling-in-synaptics-driver.html Natural scrolling in the synaptics driver], or the [https://bbs.archlinux.org/viewtopic.php?id=126258 Reverse scrolling direction ala Mac OS X Lion?] forum thread.
+
{{hc|~/.Xmodmap|<nowiki>
 +
clear      lock
 +
clear  control
 +
clear      mod1
 +
clear      mod2
 +
clear      mod3
 +
clear      mod4
 +
clear      mod5
 +
keycode      37 = Hyper_L
 +
keycode      66 = Control_L
 +
add    control = Control_L Control_R
 +
add        mod1 = Alt_L Alt_R Meta_L
 +
add        mod2 = Num_Lock
 +
add        mod3 = Hyper_L
 +
add        mod4 = Super_L Super_R
 +
add        mod5 = Mode_switch ISO_Level3_Shift
 +
</nowiki>}}
  
== Accents on US keyboards ==
+
=== Switch every number key N with Shift-N and vice-versa, for Croatian layout ===
  
The following is an example configuration:
+
Should work fine for layouts similar to Croatian as well.
 
AltGr + e -> é
 
AltGr + r -> è
 
AltGr + a -> à
 
AltGr + u -> ù
 
AltGr + i -> ì
 
AltGr + o -> ò
 
AltGr + c -> ç
 
AltGr + [ -> «
 
AltGr + ] -> »
 
AltGr + ; -> dead diaresis (ï, ü, etc.)
 
AltGr + 6 -> dead circumflex (î, ê, etc.)
 
  
This is an xmodmap file which remaps keys to match the above example.
+
{{hc|~/.Xmodmap|<nowiki>
 +
keycode 10 = exclam 1 1 exclam asciitilde dead_tilde asciitilde
 +
keycode 11 = quotedbl 2 2 quotedbl dead_caron caron dead_caron
 +
keycode 12 = numbersign 3 3 numbersign asciicircum dead_circumflex asciicircum
 +
keycode 13 = dollar 4 4 dollar dead_breve breve dead_breve
 +
keycode 14 = percent 5 5 percent degree dead_abovering degree
 +
keycode 15 = ampersand 6 6 ampersand dead_ogonek ogonek dead_ogonek
 +
keycode 16 = slash 7 7 slash grave dead_grave grave
 +
keycode 17 = parenleft 8 8 parenleft dead_abovedot abovedot dead_abovedot
 +
keycode 18 = parenright 9 9 parenright dead_acute apostrophe dead_acute
 +
keycode 19 = equal 0 0 equal dead_doubleacute doubleacute dead_doubleacute
 +
</nowiki>}}
  
clear Mod1
+
== See also ==
clear Mod2
 
!  us.map with a few redefinitions
 
keycode  9 = Escape Escape
 
keycode  10 = 1 exclam
 
keycode  11 = 2 at at
 
keycode  12 = 3 numbersign
 
keycode  13 = 4 dollar dollar
 
keycode  14 = 5 percent currency
 
keycode  15 = 6 asciicircum dead_circumflex
 
keycode  16 = 7 ampersand braceleft
 
keycode  17 = 8 asterisk bracketleft
 
keycode  18 = 9 parenleft bracketright
 
keycode  19 = 0 parenright braceright
 
keycode  20 = minus underscore backslash
 
keycode  21 = equal plus
 
keycode  22 = BackSpace Delete
 
keycode  23 = Tab Tab
 
keycode  24 = q
 
keycode  25 = w
 
keycode  26 = e E eacute
 
keycode  27 = r R egrave
 
keycode  28 = t
 
keycode  29 = y
 
keycode  30 = u U ugrave
 
keycode  31 = i I igrave
 
keycode  32 = o O ograve
 
keycode  33 = p
 
keycode  34 = bracketleft braceleft guillemotleft
 
keycode  35 = bracketright braceright guillemotright
 
keycode  36 = Return
 
keycode  37 = Control_L
 
keycode  38 = a A agrave
 
keycode  39 = s
 
keycode  40 = d
 
keycode  41 = f
 
keycode  42 = g
 
keycode  43 = h
 
keycode  44 = j
 
keycode  45 = k
 
keycode  46 = l
 
keycode  47 = semicolon colon dead_diaeresis
 
keycode  48 = apostrophe quotedbl
 
keycode  49 = grave asciitilde dead_grave
 
keycode  50 = Shift_L
 
keycode  51 = backslash bar
 
keycode  52 = z
 
keycode  53 = x
 
keycode  54 = c C ccedilla
 
keycode  55 = v
 
keycode  56 = b
 
keycode  57 = n
 
keycode  58 = m
 
keycode  59 = comma less apostrophe
 
keycode  60 = period greater quotedbl
 
keycode  61 = slash question
 
keycode  62 = Shift_R
 
keycode  63 = KP_Multiply
 
keycode  64 = Alt_L Meta_L
 
keycode  65 = space space
 
keycode  66 = Caps_Lock
 
keycode  67 = F1 F11
 
keycode  68 = F2 F12
 
keycode  69 = F3 F13
 
keycode  70 = F4 F14
 
keycode  71 = F5 F15
 
keycode  72 = F6 F16
 
keycode  73 = F7 F17
 
keycode  74 = F8 F18
 
keycode  75 = F9 F19
 
keycode  76 = F10 F20
 
keycode  77 = Num_Lock
 
keycode  78 = Scroll_Lock
 
keycode  79 = KP_7
 
keycode  80 = KP_8
 
keycode  81 = KP_9
 
keycode  82 = KP_Subtract
 
keycode  83 = KP_4
 
keycode  84 = KP_5
 
keycode  85 = KP_6
 
keycode  86 = KP_Add
 
keycode  87 = KP_1
 
keycode  88 = KP_2
 
keycode  89 = KP_3
 
keycode  90 = KP_0
 
keycode  94 = less greater bar
 
keycode  95 = F11 F11
 
keycode  96 = F12 F12
 
keycode 108 = KP_Enter
 
keycode 109 = Control_R
 
keycode 112 = KP_Divide
 
keycode 113 = Mode_switch
 
keycode 114 = Break
 
keycode 110 = Find
 
keycode  98 = Up
 
keycode  99 = Prior
 
keycode 100 = Left
 
keycode 102 = Right
 
keycode 115 = Select
 
keycode 104 = Down
 
keycode 105 = Next
 
keycode 106 = Insert
 
keycode 116 = Mode_switch
 
! right windows-menu key, redefined as Compose key
 
keycode 117 = Multi_key
 
add Mod1 = Alt_L
 
add Mod2 = Mode_switch
 
  
== Additional resources ==
 
 
*[http://www.x.org/archive/current/doc/man/man1/xmodmap.1.xhtml Current man page] at X.Org Foundation
 
*[http://www.x.org/archive/current/doc/man/man1/xmodmap.1.xhtml Current man page] at X.Org Foundation
 
*[http://cweiske.de/howto/xmodmap/allinone.html Multimediakeys with .Xmodmap HOWTO] by Christian Weiske
 
*[http://cweiske.de/howto/xmodmap/allinone.html Multimediakeys with .Xmodmap HOWTO] by Christian Weiske
 
*[http://dev-loki.blogspot.com/2006/04/mapping-unsupported-keys-with-xmodmap.html Mapping unsupported keys with xmodmap] by Pascal Bleser
 
*[http://dev-loki.blogspot.com/2006/04/mapping-unsupported-keys-with-xmodmap.html Mapping unsupported keys with xmodmap] by Pascal Bleser
*[http://en.gentoo-wiki.com/wiki/Multimedia_Keys Multimedia Keys article] on the [http://en.gentoo-wiki.com/ Gentoo Wiki]
 
*[http://keytouch.sourceforge.net/howto_keyboard/node4.html How to retrieve scancodes] by Marvin Raaijmakers
 
 
*[http://wiki.linuxquestions.org/wiki/List_of_Keysyms_Recognised_by_Xmodmap List of Keysyms Recognised by Xmodmap] on [http://linuxquestions.org LinuxQuestions]
 
*[http://wiki.linuxquestions.org/wiki/List_of_Keysyms_Recognised_by_Xmodmap List of Keysyms Recognised by Xmodmap] on [http://linuxquestions.org LinuxQuestions]

Latest revision as of 01:24, 29 March 2017

xmodmap is a utility for modifying keymaps and pointer button mappings in Xorg.

xmodmap is not directly related to X KeyBoard extension (XKB), as it uses different (pre-XKB) ideas on how keycodes are processed within X. Generally, it is only recommended for the simplest tasks. See X KeyBoard extension for advanced layout configuration.

Note: xmodmap settings are reset by setxkbmap, which not only alters the alphanumeric keys to the values given in the map, but also resets all other keys to the startup default. [1]

Introduction

There are two types of keyboard values in Xorg: keycodes and keysyms.

keycode
The keycode is the numeric representation received by the kernel when a key or a mouse button is pressed.
keysym
The keysym is the value assigned to the keycode. For example, pressing A generates the keycode 73, which is mapped to the keysym 0×61, which matches A in the ASCII table.
The keysyms are managed by Xorg in a table of keycodes defining the keycode-keysym relations, which is called the keymap table. This can be shown by running xmodmap.

Installation

xmodmap can be installed through the xorg-xmodmap package from the official repositories.

Optionally, install xkeycaps, which is a graphical front-end to xmodmap.

Keymap table

Print the current keymap table formatted into expressions:

$ xmodmap -pke
[...]
keycode  57 = n N
[...]

Each keycode is followed by the keysym it is mapped to. The above example indicates that the keycode 57 is mapped to the lowercase n, while the uppercase N is mapped to keycode 57 plus Shift.

Each keysym column in the table corresponds to a particular combination of modifier keys:

  1. Key
  2. Shift+Key
  3. mode_switch+Key
  4. mode_switch+Shift+Key
  5. AltGr+Key
  6. AltGr+Shift+Key

Not all keysyms have to be set, but to assign only a latter keysym, use the NoSymbol value.

To see which keycode corresponds to a key, see Extra keyboard keys#In Xorg for details on the xev utility which will output relevant keycode/keysym information about a key when you press it.

Tip: There are predefined descriptive keysyms for multimedia keys, e.g. XF86AudioMute or XF86Mail. These keysyms can be found in /usr/include/X11/XF86keysym.h. Many multimedia programs are designed to work with these keysyms out-of-the-box, without the need to configure any third-party application.

Note that xmodmap is influenced by xkbd settings, so all eight keysym are available for the us(intl) xkbd layout but not for the default us (it is missing the ralt_switch symbol defined in level3). To have all 8 keysyms available you should configure the (intl) variant of the keyboard from xorg.conf or add, using us layout as an example, setxkbmap -layout 'us(intl)' before calling xmodmap.

Custom table

To create a key map (i.e. ~/.Xmodmap):

$ xmodmap -pke > ~/.Xmodmap

To test the changes:

$ xmodmap ~/.Xmodmap

Activating the custom table

With GDM, XDM, KDM or LightDM there is no need to source ~/.Xmodmap. For startx, use:

~/.xinitrc
[[ -f ~/.Xmodmap ]] && xmodmap ~/.Xmodmap

Alternatively, edit the global startup script /etc/X11/xinit/xinitrc.

Test changes

To make temporary changes:

$ xmodmap -e "keycode  46 = l L l L lstroke Lstroke lstroke"
$ xmodmap -e "keysym a = e E"

Modifier keys

xmodmap can also be used to override modifier keys, e.g. to swap Control and Super (the Windows keys).

Before assignment the modifier keys need to be empty. ! is a comment, so only the modifiers Control and Mod4 get cleared in the following example. Then the keysyms Control_L, Control_R, Super_L and Super_R are assigned to the opposite modifier. Assigning both left and right to the same modifier means that both keys are treated the same way.

~/.Xmodmap
[...]
!clear Shift
!clear Lock
clear Control
!clear Mod1
!clear Mod2
!clear Mod3
clear Mod4
!clear Mod5
!add Shift   = Shift_L Shift_R
!add Lock    = Caps_Lock
add Control = Super_L Super_R
!add Mod1    = Alt_L Alt_R
!add Mod2    = Mode_switch
!add Mod3    =
add Mod4    = Control_L Control_R
!add Mod5    =
Note: The example assumes that the Control_L and Control_R keysyms were assigned to the Control modifier, and Super_L and Super_R keysyms to the Mod4 modifier. If you get the following error message X Error of failed request: BadValue (integer parameter out of range for operation), you will need to adapt accordingly. Running xmodmap produces a list of modifiers and keys that are assigned to them.

The following example modifies CapsLock to Control, and Shift+CapsLock to CapsLock:

~/.Xmodmap
clear lock
clear control
add control = Caps_Lock Control_L Control_R
keycode 66 = Control_L Caps_Lock NoSymbol NoSymbol

Reverse scrolling

Merge-arrows-2.pngThis article or section is a candidate for merging with Mouse buttons.Merge-arrows-2.png

Notes: xmodmap is not the only way to do this. (Discuss in Talk:Xmodmap#)

The natural scrolling feature available in OS X Lion (mimicking smartphone or tablet scrolling) can be replicated with xmodmap. Since the synaptics driver uses the buttons 4/5/6/7 for up/down/left/right scrolling, you simply need to swap the order of how the buttons are declared in ~/.Xmodmap:

~/.Xmodmap
pointer = 1 2 3 5 4 7 6 8 9 10 11 12

Then update xmodmap:

$ xmodmap ~/.Xmodmap

Swapping mouse buttons

Merge-arrows-2.pngThis article or section is a candidate for merging with Mouse buttons.Merge-arrows-2.png

Notes: xmodmap is not the only way to do this. (Discuss in Talk:Xmodmap#)

The left, middle and right mouse buttons correspond to buttons 1,2 and 3 respectively in the synaptics driver. To swap left and right mouse buttons, again simply reverse the order in which they are listed in your ~/.Xmodmap:

~/.Xmodmap
pointer = 3 2 1

This should suffice for a simple mouse setup. Again, update xmodmap:

$ xmodmap ~/.Xmodmap

Templates

Spanish

~/.Xmodmap
keycode  24 = a A aacute Aacute ae AE ae
keycode  26 = e E eacute Eacute EuroSign cent EuroSign
keycode  30 = u U uacute Uacute downarrow uparrow downarrow
keycode  31 = i I iacute Iacute rightarrow idotless rightarrow
keycode  32 = o O oacute Oacute oslash Oslash oslash
keycode  57 = n N ntilde Ntilde n N n
keycode  58 = comma question comma questiondown dead_acute dead_doubleacute dead_acute
keycode  61 = exclam section exclamdown section dead_belowdot dead_abovedot dead_belowdot
!Maps the Mode key to the Alt key
keycode 64 = Mode_switch

Turn CapsLock into Control

Simplest example of changing CapsLock into Control.

~/.Xmodmap
clear lock
clear control
keycode 66 = Control_L
add control = Control_L Control_R

Turn CapsLock into Control, and LeftControl into Hyper

Laptop users may prefer having CapsLock as Control. The Left Control key can be used as a Hyper modifier (an additional modifier for emacs or openbox or i3).

~/.Xmodmap
clear      lock 
clear   control
clear      mod1
clear      mod2
clear      mod3
clear      mod4
clear      mod5
keycode      37 = Hyper_L
keycode      66 = Control_L
add     control = Control_L Control_R
add        mod1 = Alt_L Alt_R Meta_L
add        mod2 = Num_Lock
add        mod3 = Hyper_L
add        mod4 = Super_L Super_R
add        mod5 = Mode_switch ISO_Level3_Shift

Switch every number key N with Shift-N and vice-versa, for Croatian layout

Should work fine for layouts similar to Croatian as well.

~/.Xmodmap
keycode 10 = exclam 1 1 exclam asciitilde dead_tilde asciitilde
keycode 11 = quotedbl 2 2 quotedbl dead_caron caron dead_caron
keycode 12 = numbersign 3 3 numbersign asciicircum dead_circumflex asciicircum
keycode 13 = dollar 4 4 dollar dead_breve breve dead_breve
keycode 14 = percent 5 5 percent degree dead_abovering degree
keycode 15 = ampersand 6 6 ampersand dead_ogonek ogonek dead_ogonek
keycode 16 = slash 7 7 slash grave dead_grave grave
keycode 17 = parenleft 8 8 parenleft dead_abovedot abovedot dead_abovedot
keycode 18 = parenright 9 9 parenright dead_acute apostrophe dead_acute
keycode 19 = equal 0 0 equal dead_doubleacute doubleacute dead_doubleacute

See also