How do I remap keyboard keys for Ubuntu

In this post, learn how to change the default behavior of keys on the keyboard in Ubuntu, especially useful for multimedia keys.

Newer versions of Ubuntu use xkb for keyboard layouts. xkb works with a hierarchy of multiple files to handle different keyboard settings. New entries can be added to one main config file which can change the behavior of the keys or in other words keys can be remapped.

In this how to I will be changing the top row keys on my Chromebook, but you can use this method for your laptop or desktop keyboard. Wouldn’t it be nice to have multimedia keys on your old keyboard?

Setting up the Chromebook F keys in 3 steps

Update: If you are using this guide for your Chromebook, see this post which will do this automatically for you.

A fresh install of Ubuntu on a chromebook maps the top row keys to the F keys (F1, F2 …). Let’s change them to match their button icons.

On chromebooks there is no caps lock key and in it’s place is a search key. Let’s change that as well.

Step 1: Find keyboad identifiers

Each key has an identifier that needs to be used to tell the system what to do when the key is pressed. For example, the ‘XF86MonBrightnessDown’ tells the key to turn down the screen brightness when pressed.

We can use the xmodmap utility to find these out:

  • Open a terminal window (Ctrl + Alt + T)
  • Type xmodmap -pke
  • You will see a rather long list of all the keyboard identifiers.
  • For convenience let’s make a copy of these identifiers so that we can refer to the contents later.
  • Type xmodmap -pke > ~/keymaptable
  • You will find the new file called keymaptable in your home folder.

Step 2: Add new entries to the main xkb config file

  • In your terminal window, open the xkb file with gedit. Type sudo gedit /usr/share/X11/xkb/symbols/pc
  • You will see various entries in this file that are surrounded by squiggly brackets. This is where we want to add our new entries to override the existing ones.
  • I want my F keys to be remapped like so:

  • Paste these entries into the config file in between the squiggly brackets that define your keyboard (see below).
  • key <FK01> { [XF86Back] };
    key <FK02> { [XF86Forward] };
    key <FK03> { [XF86AudioPlay] };
    key <FK04> { [Print] };
    key <FK05> { [Super_L] };
    key <FK06> { [XF86MonBrightnessDown] };
    key <FK07> { [XF86MonBrightnessUp] };
    key <FK08> { [XF86AudioMute] };
    key <FK09> { [XF86AudioLowerVolume] };
    key <FK10> { [XF86AudioRaiseVolume] };
    key <LWIN> { [Caps_Lock] };
  • Save the file.

Step 3: Clear the xkb settings cache

  • Type sudo rm -rf /var/lib/xkb/* in your terminal.
  • Reboot your computer/laptop.

 

When you log back in you should find that the keys have been remapped, pretty useful huh?

You can of course set the F keys to whatever you want to. Just look in your keymaptable file you created earlier for key identifiers that match your preferred action.

So, what are you waiting for? Make your own customised keyboard!

Links:



41 Comments

  • Thanks for the post. New to Linux here. Been a great help in getting my ubuntu keyboard layout similar to what is on the hardware of the Chromebook.
    I was wondering if you have found a way to set a single key for full screen. Ideally setting the F4 as that key, i.e. playing the same role F11 does on most keyboards. I saw you answer a similar question earlier on in the comments.
    Was wondering if you have found a solution to this or if not do you have any ideas on an alternate method to go full screen whilst in browsers?
    Thanks,
    Aaron

    1. Hi Aaron, I was hoping that someone else might have found a solution, it’s not something I’ve currently got time to test, sorry.

  • Thanks! I needed to interchange my “end”, “page up” and “page down” bindings and it worked just fine! Very easy to do.

  • I followed the steps and remapped the f1 key to ‘d’ and after restarting my pc the keyboard stopped working. 🙁

    1. Hi Sanya, you have probably made a typo in your keyboard config file, so when you reboot, the keyboard doesn’t work.

      You can either chroot into the system using a live USB drive or reinstall your distro.

  • I’m struggling to get the keyboard backlight to work on a 2015 Chromebook 2 with Mint. I managed to get it working befoire with a slider applet but had to reinstall Mint and can’t remember what I did. Has anyone else got the backlight functioning.

  • Hey there. I know this is an old thread/post but it was the ONLY way I found to get a delete key on my Asus Chromebook Flip! Thank you so much. I haven’t tried all the F keys (the script you link to for automating Chrombook didn’t work for me) but I have the “Full Screen” key working anyway so that’s fine. The only other key I need (well, would like) is the Caps Lock. I tried just updating the CAPS to LWIN as per above but with no joy – it just opened my Google Apps window… And this is probably the problem as I run my Crouton in -X xiwi so I run in a tab which is probably the problem. Are you able to confirm that this is the case? In which case I may try and find another option for Caps Lock… Any suggestions? Thanks again for giving me my beloved delete key back 😀

    1. Yes, in a tab the key is responding to ChromeOS and not your Linux install.

      I haven’t used crouton for over a year but it sounds like that key isn’t being seen as the LWIN key in Linux perhaps?

  • The following approach is simple and easy. It’s what I use running GalliumOS 2.0 on an Acer C720.

    1. Open a Terminal
    2. Type”xev”
    3. Press enter
    4. Press various keys on your keyboard
    5. Notice the keycodes that appear in the terminal
    6. Navigate to your home directory
    7. Create a text file named “.Xmodmap”
    8. Open “.Xmodmap”
    9. Map keycodes to commands
    10. Logout or restart your computer
    11. Smile!

    Below are some keycodes I mapped:

    ! ~/.Xmodmap

    keycode 51 = emdash endash
    keycode 67 = Home
    keycode 68 = End
    keycode 69 = Page_Up
    keycode 70 = F11
    keycode 71 = Page_Down
    keycode 124 = Delete
    keycode 121 = XF86AudioMute
    keycode 122 = XF86AudioLowerVolume
    keycode 123 = XF86AudioRaiseVolume
    keycode 72 = XF86MonBrightnessDown
    keycode 73 = XF86MonBrightnessUp

    The above has one little glitch:

    “When NumLock is on, brightness keys and power button don’t work. xev reports different keypresses for these keys with NumLock on or off… ” Source: https://github.com/GalliumOS/galliumos-distro/issues/197

    To workaround this glitch I ensure NumLock on my external keyboard is off when I want to change the brightness settings.

    1. Hi Yek,

      GalliumOS already maps the keys for you. The method of using a .Xmodmap config file doesn’t work in all distros in my initial testing anyway (I wrote this post way back in Oct 2015).

      As with all things Linux there are many different ways to get things working and I’m sure this will help some readers. Thanks for posting!

  • Hi, I have a Chromebook with GalliumOS and though there’s a universal fullscreen key (F4), I’d like to turn F5 (the key with the square and ||) into F11, to full screen on browsers. Could you help with this?

    1. Hi Daniel,

      I’ve had a look and can’t find the xbd symbol for full screen, otherwise you would be able to just assign that to the F5 key using the method described in this post. Sorry.

  • Hey, thanks for putting this out here, but in my case I rebooted and my keyboard doesn’t work any longer. I’m guessing this means I’m hosed and have to redo everything starting with the install, but I’m wondering what could have caused that. Does it interact badly with encrypted drives, or did I put it in the wrong spot within the file? Sorry, very inexperienced user here, just wanting to get a delete key and other stuff running on my ubuntu-C720. Thanks!

    1. Hi Kurt,

      It sounds like you have an incorrect setting in the keyboard config file which is why the keyboard doesn’t work anymore. I’ve done this myself when I was testing and sorted it out using chroot. If you’re inexperienced you’re better of doing a fresh install of Linux and running my script that will make the keys work for you.

      1. Thanks very much! In the end, what was driving this for me was a desire to have a delete key on my C720. I’ll definitely run that script to get all the functionality in the top row keys working and get the F keys back, but do you know of a way I could get a delete key by something like Shift+Backspace or something? Thanks again for making things like this script available!

        1. Do the following in a terminal window:

          sudo gedit /usr/share/X11/xkb/symbols/pc

          On the line near the top of the file (about the 10th line) you’ll see the BKSP definition as Backspace, Backspace. Change that to Backsace, Delete. Save the file and run the following command:

          sudo rm -rf /var/lib/xkb/*

          After a reboot, SHIFT + Backspace will work as the delete key.

  • hi sir, could you please advice me why the following does’nt work ..

    key { [Home] };
    key { [End] };

    thank you very much.

    1. i refer to my previous posting.

      i dont know why this system deleted my “greater than” and “lower than” signs and the text between them. this made my previous posting read so stupid.

      what i actually did earlier was i assigned the pause key to “home” and the insert key to “end”, and it didnt work.

      the syntax i used was exactly as told.

      please advice

      thank you very much.

      1. You’re right the commenting system I’m using is removing the ‘greater than’ and ‘lower than’ signs and the text between them, sorry!.
        If it’s not working it’s maybe you forgot to run the command sudo rm -rf /var/lib/xkb/* or your pause and insert keys are modifier map definitions that I haven’t been able to figure out.
        Maybe you could use other keys instead of the pause and insert keys?

  • hi sir, may i ask how do you come up with keyword FK01 and so on to FK10 ? can you please share the available keywords please.

    thank you very much.

    1. Hi Joseph,
      If you look in /usr/share/X11/xkb/keycodes/xfree86 you will see all the key definitions like AE01, BKSP, AB05 and so on. Some of the names are obvious like BKSP for the backspace key. Others like AE01 might not be.

      All alphanumeric characters start with A and the next letter represents the row that the key is in. The bottom row on the keyboard (the row with the spacebar) is row A, the next row up is row B and so on.

      So for AE01:
      (AE01) – alphanumeric key
      (AE01) – in row E
      (AE01) – is the first key in that row

      That makes AE01 the Q on most keyboard layouts.

      Hopefully that makes sense.

      1. thank you very much for replying, and that was very quick too.

        i am new to linux and my keyboard need to use 2 fingers to use the “home” and “end” key which i hate very much. thats the reason i’m here.

        thank you very much again for your help.

        1. Ok, you need to pick a key to use for home and end so if you chose F1 and F2, you would use:

          key (FK01) { [ Home ] };
          key (FK02) { [ End ] };

          (replace the brackets with the arrow brackets, it appears that they are getting deleted by the commenting system.

  • unfortunately, that won’t work, since higher up in that file it includes altwin function… I had to add/change the control and alt functionality in there. It works, except for Alt+Tab to switch windows unfortunately. Which I use often…

    1. You can just add LALT and RCTL to the xkb definitions as described in this post and match them to what you want them to do.

  • Thanks a lot! Any suggestion on how to access the original Function keys (so get F10 with the “search”+”volumeup” button)?

    1. Hi Willem, I think you would use the modifier map definitions but I haven’t worked out how yet!

  • This is the closest I can find to what Id like to do although I have keys on my keyboard that arent being used and Id like to assign then to something for example Id like to use an unused key next to the spacebar as a right super key and so on. Question is, with the above table and config file, I have blank keycodes arent clear to map them on my keyboard and in the xkb file i dont seem to find those blank keys there at all. This seems to be the way to go about swapping keys around and modifying keys that are already being detected and have functions, i want to add keys that arent being detected and have no functions (adding functions like duplicating another keys function to these unassigned keys).
    Anyone able to help? It will make this a ton easier.

  • I followed these exact instructions. Everything appears to be working except for the F6 key which is supposed to lower the brightness. I assume others have not experienced this issue?

    1. That’s strange, I’ve just checked and F6 for brightness still works for me. Maybe double check for any typos. Also check xmodmap -pke to verify the brightness up identifier.

    2. I am having a same experience with Ubuntu Mate 16.04.
      When I tried this is Elementary OS, it worked fine.

      1. I have not tried Ubuntu Mate, but can confirm that sound and touchpad work if I do a clean install and then not run the system updates.

  • Hi, your website is brilliant. Got ubuntu working on the chromebook in one evening. Left a 50 euro donation to John Lewis. I have been trying to get this done since June, when I bough the chromebook. Can you help with one more thing – skype! The damn thing does not work, tried quite a few recommended fixes.

    1. I’m glad you like the site, words of encouragement are always welcome 🙂 Well done for donating to John, I think he more than deserves it. As for Skype, you need to enable the Canonical Partners repository. You can do this by going to Software and Updates > Other Software and ticking the boxes for Canonical Partners. You will then be able to install Skype from Software Centre. I haven’t been able to find a fix for the mic though so you’ll have to try an external mic.

  • Comments are closed.