283

I'm searching for a way to remap certain keys in ubuntu.

i.e.

I'd like to change PgUp to Home or PgDown to End.

Does a built-in command or a tool exist reassign keys in Ubuntu/GNOME?

Tim
  • 32,861
  • 27
  • 118
  • 178
NES
  • 33,195
  • 1
    checkout my answer here. It doesn't worth copy and paste same answer. Perhaps it would help you. – Rahul Virpara Jun 02 '12 at 09:40
  • 2
    Any Emacs-like bindings here for CTRL-P/N for unit steps? – Léo Léopold Hertz 준영 Jul 07 '15 at 13:12
  • 2
    Since some time xmodmap is depricated! to get a system wide setting you have to use xkb. So edit the language file in /usr/share/X11/xkb/symbols/ to add your changes there.

    See http://askubuntu.com/a/898462/34298

    – rubo77 Mar 30 '17 at 21:48
  • If you want to remap your keyboard keys or mouse buttons to certain keys, use "Input Remapper" by sezanzeb. It's VERY simple, it has a GUI, and it just WORKS. I just have set a certain shortcut to simulate a keyboard key, works well. – Allexj Jan 07 '23 at 12:54

11 Answers11

291

Notice: As of 2013, Ubuntu and derivatives no longer use xmodmap, but instead use xkb. For more information see this answer. The answer below is no longer relevant for current releases.


For remapping certain keys you need two tools. First xev (command-line tool) and second xmodmap (also command-line tool). Both should be available in Ubuntu without extra installing them.

  1. Start terminal window and run xev. Now it's active and waits for you to press a key. Then press the key whose behaviour you want to change. i.e. PgUp.

  2. xev will output some information about the pressed key. The third line is important. It should look similar to:

     state 0x10, keycode 110 (keysym 0xff55, Prior), same_screen YES,
    

    in this example Prior is the name of the behaviour the key is assigned to at the moment, the number keycode is the internal id to recognize the key. Now do this with another key i.e. PgDown give this output

     state 0x10, keycode 115 (keysym 0xff56, Next), same_screen YES,
    

    Here again the interesting part for us is keycode 115 and Next - the name of the behaviour.

  3. now when you want to swap the two keys use xmodmap.

      xmodmap -e "keycode 110 = Next"
    

    This changes the key with keycode 110 on your keyboard to the action Next. It's pretty simple.

    Note that if the key you are mapping should have a different meaning when used with the Shift key (for example for British keyboard layouts, Shift+2 gives quotation marks) then you can simply list the secondary command after the first. For example if you want the key with code 53 to map to backslash normally, but to the bar symbol when used with shift, you might do:

      xmodmap -e "keycode 53 = backslash bar"
    

Additional information: The sequence of these mappings depends on the keyboard layout. It usually is Key, Shift+Key, mode_switch+Key, mode_switch+Shift+Key, AltGr+Key, AltGr+Shift+Key, but can be very different for more special layouts, like in case of the German Neo 2 one. To skip a column use NoSymbol. Here is a comprehensive list of all keysyms.
You can see the concrete sequence for your layout by finding it in one of the files in /usr/share/X11/xkb/symbols/.

Note: These change are for the active X session only and will be lost after reboot. When you want to save the changes permanently you have to run the following commands after the ones above:

xmodmap -pke >~/.Xmodmap

(it creates a file named .Xmodmap in your home directory (~))

Then you have to create a file named .xinitrc in your home directory where you put command xmodmap .Xmodmap in.

You can now modify .Xmodmap and run xmodmap .Xmodmap from console to see the changes immediately. The changes in .Xmodmap will persist.

source: Ubuntu Foruns

Bonus stuff:

If the key you are remapping has different behavior depending on a state ( like how the keys in the numeric keyboard depend on NumLock) you simply have to do xmodmap -pm to get a list of modifiers and then do:

xmodmap -e "KEYCODE MODIFIER = behaviour behaviour_with_modifier"

Suppose, for example, that you want to get a period instead of a comma on the numeric keyboard (useful for most programmers), but you want to keep the "delete" behavior when NumLock is off.

xmodmap -e "keycode 91 mod2 = KP_Delete period"

mod2, because xmodmap -pm tells us that mod2 is Num_Lock, the other names are obtained by pressing the keys in xev.

NES
  • 33,195
  • 6
    xev is not able to catch Fn key pressings – om-nom-nom Feb 15 '12 at 15:08
  • 3
    The selected answer didn't work for remapping the Caps-lock key for me on Ubuntu 12.10.

    I was able to to do this by going to System Settings -> Keyboard -> Layout Settings -> Options, which has a list of keys and alternative behaviors. Worked flawlessly in Unity and terminal.

    – Allyl Isocyanate Oct 10 '12 at 19:17
  • 1
    If you have multiple keyboards connected, will these tools see a difference between the same key on another keyboard? – jobukkit Jun 27 '13 at 17:10
  • @Jop I don't think so, since what you are changing is the response to the keycode and the keycodes of the to keyboards should overlap. – John C Jul 28 '13 at 00:28
  • 1
    Strange, I did everything, but my keymappings still reset after restarting. – Costa Michailidis Dec 27 '13 at 03:39
  • 1
    I don't think I have an -event flag in Ubuntu 12.04. It throws an error and doesn't mention "event" in man xev. – isomorphismes Feb 17 '14 at 14:47
  • fwiw: after logging the results of xev to a file called event.out I filter the non-keypress events with perl -e '$/ = "\n\n"; while(<>) { print $_ if $_ =~ "KeyPress"; }' event.out > keypress.event.out. – isomorphismes Feb 17 '14 at 15:05
  • 1
    xmodmap -e "KEYCODE MODIFIER = behaviour behaviour_with_modifier" ... Where did you get this from? It doesn't seem to work, and I can't find any other reference / docs on this syntax ... – Martin Tournoij Mar 08 '14 at 23:01
  • This answer is still relevant for Ubuntu 14.04 and works just fine, you just have to run the commands each time you login. – jmiserez May 31 '16 at 13:07
  • 1
  • If .xinitrc does not work, try .xsessionrc instead;
  • Avoid setxkbmap overwriting xmodmap: http://askubuntu.com/questions/451945
  • – updogliu Dec 12 '16 at 00:11
  • ubuntu server 18.04, without LightDM/GDM and Desktop Environment, simply startx and xmodmap -e in .xinitrc: xmodmap works. – frozen-flame Oct 12 '20 at 00:16
  • I am able to disable Function keys like F1 using the command xmodmap -e 'keycode 67=' . But need to disable a combination like "Control Alt F1", how to do that ? – Subhajit Feb 02 '22 at 15:14
  • @om-nom-nom: Fn is a special hardware key that is meant to be handled by low-level software like firmware, and work even if there is no operating system running at all or it isn’t even officially on. Remapping it can be done, but it required changing said firmware. Which hopefully will be open source if the owner was wise. :) –  Aug 17 '22 at 18:30
  • 1
    @Subhajit: If keycode 67 isn’t mapped to F1 anymore, there is no way to cause a Control-Alt-F1 keypress anymore either, nor any oney combo with F1 in it, as there is no F1 key to press, unless there is another keyboard or another keycode is mapped to F1. –  Aug 17 '22 at 18:31
  • If you want to remap your keyboard keys or mouse buttons to certain keys, use "Input Remapper" by sezanzeb. It's VERY simple, it has a GUI, and it just WORKS. I just have set a certain shortcut to simulate a keyboard key, works well. – Allexj Jan 07 '23 at 12:55