27

This is working for the current session:

costales@dev:~/Desktop$ xmodmap -e "keycode 166 = less"
costales@dev:~/Desktop$ xmodmap -e "keycode 167 = greater"

Then I created this config file:

costales@dev:~/Desktop$ cat ~/.Xmodmap 
keycode 166 = less
keycode 167 = greater
costales@dev:~/Desktop$ 

But it's not working after a reboot. How could I force the remap to survive reboot?

15

I've been using 16.04 for a little bit and it seems to use sddm as its desktop manager. It used to be LightDM and GDM before that. Both its predacessors are documented to load ~/.Xmodmap automatically but I can't find anything explicitly saying sddm does.

Therefore it may be advisable to just force it to load with a new script. You just need to run xmodmap ~/.Xmodmap and you can do that a number of ways:

There are probably a few dozen other ways to manage this, essentially doing the same thing.

| improve this answer | |
  • 3
    Thanks for the tip! Add to start up didn't work and ~/.xinitrc doesn't exit. But append "xmodmap ~/.Xmodmap" to ~/.bashrc worked! :) – Costales Mar 24 '16 at 11:43
  • 1
    Adding it via the graphical start up tools (which creates a new entry in ~/.config/autostart) works about half the time. I've rebooted about 10 times now and 5 times the command has been executed and the other 5 times it hasn't. :-( – Alexander Rechsteiner Jun 17 '16 at 11:17
  • 1
    @Alexander Rechsteiner: This seems to be a problem in Ubuntu 16.04. I've been fiddling with startup commands and key remapping, and the behavior seems very inconsistent. I'm not an expert in this type of thing, but it seems utterly ridiculous for it to be happening... – nicorellius Feb 26 '17 at 17:58
8

I added a file /etc/X11/Xsession.d/80_xmodmap with these contents:

#!/bin/sh
# Set custom keycodes
#
# This file is sourced by Xsession(5), not executed.
# The "|| true" is to ensure that the Xsession script does not terminate on error

USRMODMAP="$HOME/.Xmodmap"

if [ -x /usr/bin/xmodmap ]; then
        if [ -f "$USRMODMAP" ]; then
                /usr/bin/xmodmap "$USRMODMAP" || true
        fi
fi

This works every time.

| improve this answer | |
  • It appears to work but this is the messages I get in .xsessions-errors: .Xmodmap:1: bad keysym in remove modifier list 'Caps_Lock', no corresponding keycodes this is my .Xmodmap: $ cat .Xmodmap remove Lock = Caps_Lock keycode 9 = Escape keycode 66 = Escape – MaikoID Oct 25 '16 at 19:16
8

In my long experience with remapping keys in Ubuntu, the permanent solution is to modify a file called evdev in /usr/share/X11/xkb/keycodes.

Run this command in the command prompt.

sudo gedit /usr/share/X11/xkb/keycodes/evdev

Remember gedit is a Ubuntu text editor so you can use nano or vim instead of gedit.

The file is in the format: ALIAS: CODE. You can swap buttons by interchanging codes of different keys. For example, to swap RCONTROL with RETURN, edit two lines in evdev into this:

<RTRN> = 105;

<RCTL> = 36;

To know the number code for a button run xev on the terminal.

Refer to the documentation for more information. https://buildmedia.readthedocs.org/media/pdf/python-evdev/latest/python-evdev.pdf

| improve this answer | |
  • 2
    I think this might the easiest way to permanently remap a key. I just did that successfully on a Ubuntu 18.04-based distribution, replacing my dead up arrow key by the right shift key. Thanks! – stragu Dec 28 '19 at 13:22
  • I found that guide particularly helpful when editing evdev: charvolant.org/doug/xkb/html/xkb.html – Mario Jul 15 at 15:33
  • And, yes, in Ubuntu 19.10, the xmodmap-based solution doesn't work persistently for me, as some background process(es) randomly runs setxkbmap and resets everything. Placing xmodmap in .xinitrc or so doesn't seem to fix that. – Mario Jul 15 at 15:37
  • @Mario Yep! It doesn't. What I would do is to place all the xmodmap commands in a single file and then execute all the commands in the file at once on bootup. – GilbertS Jul 15 at 17:24
5

open gnome-session-properties. add command and name. desktop file will be created in ~/.config/autostart.

remenber: use absolute path, eg /home/xxx/.Xmodmap. never use '~' as your home dir.

| improve this answer | |
2

Navigate to

/usr/share/X11/xkb/symbols

With sudo privileges, in an editor, open the layout you desire to edit. Replace the values.

After reboot, the changes will be applied.


Here is an example I use to remap my keypad because of keyboard hardware issues.

keypad remap example

| improve this answer | |
  • 2
    Those edits will be overwritten the next time the packages that owns those files get upgraded. – muru Nov 26 '17 at 13:13
  • That may be true, but I doubt these specifically are updated very often. I have been satisfied with this solution for some months since I solved the issue and not once did I have to do anything additional. And to prevent this, you can save the file and replace it in the case of such event or, (which seems more secure) just recreate the process. – quaeched Nov 26 '17 at 13:16
1

Graphical Editor

gnome-tweaks > Keyboard & Mouse > Additional Layout Options

Key Maps

| improve this answer | |
1

After searching on Google, I was able to resolve my laptop faulty keys remapping. All faulty keys on my Thinkpad T400 are working fine after remapping. Also, it's permanent without an extra load on the CPU core.

  • First find your keyboard layout or keycode using below command.

    xev -event keyboard 
    
  • For example, on my Thinkpad T400 keycode found as follows:

    Keycode of "z" key is 52
    Keycode of "x" key is 53
    Keycode of "c" key is 54
    Keycode of "v" key is 55
    Keycode of "F6" key is 72
    Keycode of "F10" key is 76
    Keycode of "F11" key is 95
    Keycode of "F12" key is 96 
    
  • Remapping character keys "z" "x" "c" "v" by assigning function keys "F6" "F10" "F11" "F12".

  • Modify lines as below (Backup file before making any changes to rollback easily if things go wrong).

    sudo vi /usr/share/X11/xkb/keycodes/evdev
    
  • Find the lines and change value as below:

    <AB01> = 72;
    <AB02> = 76;
    <AB03> = 95;
    <AB04> = 96;
    
  • Comment these lines or remove the lines:

    //  <FK06> = 72;
    //  <FK10> = 76;
    //  <FK11> = 95;
    //  <FK12> = 96;
    
  • Save and exit.

To remap special keys:

  • Backup file before making changes to rollback if things go wrong.

    cd /usr/share/X11/xkb/symbols/
    
    sudo vi pc
    
  • Add or modify below lines in function "xkb_symbols "editing" {"

    key [SCLK] {    [  Prior        ]   };
    key [PAUS] {    [  Next         ]   };
    
  • Save and exit.

  • Clear cache.

    sudo rm -rf /var/lib/xkb/*
    
  • Reboot.

| improve this answer | |
  • I remapped a broken (cursor) arrow key to another key using this technique. It's simply (xev) and press new key to get its scancode, then edit /usr/share/X11/xkb/keycodes/evdev to replace the arrow key's original keycode with the new keycode and to remove the new arrow key's line with its original scancode. I commented wherever I made a change and kept a backup of the file. After a reboot it works perfectly, including shift/ctrl/alt combinations with the new arrow key. And the change should be left in place even when upgrading packages and OS version upgrades. – Matthew Oct 29 '18 at 20:47
0

Because .Xmodmap files are much easier to edit than xkb, I use .Xmodmap as a 'source' file, then convert it to xkb (the current standard) and auto-load at boot using the .bashrc method.

It's a few simple steps posted here.

Works perfectly, and using xkb future-proofs against when support for .Xmodmap is dropped.

| improve this answer | |
  • Why do you think support for xmodmap will be dropped? – Olaf Dietsche Feb 16 at 11:04
  • Thanks for the heads-up @OlafDietsche. Just completed upgrades to 18.04.3 on 3 machines and was surprised to find xmodmap support not only returned, but better than before. So I'll update the link and the above post w/ current info. Best to you. – u2n Feb 18 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.