41

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?

Organic Marble
  • 23,641
  • 15
  • 70
  • 122
Costales
  • 2,278

9 Answers9

27

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.

Log out and back in to apply changes.

jtpereyda
  • 2,065
GilbertS
  • 371
  • 3
    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
  • 1
    I found that guide particularly helpful when editing evdev: https://www.charvolant.org/doug/xkb/html/xkb.html – mfg Jul 15 '20 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. – mfg Jul 15 '20 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 '20 at 17:24
  • 1
    HOORAY! I've been struggling with this for hours, and this is what finally worked. In my case I wanted my web key to be my menu key, so I made <COMP> = 180 and commented out the line where <I180> = 180. Downside is that if I plug in a different keyboard, the real Menu key won't work, but the compact keyboard I'm using doesn't have one at all. – Will Matheson Sep 02 '20 at 18:19
  • @WillMatheson Yes! Very true. The changes will affect all keyboards. What you could do is to have two keys do the same thing so that the old menu key and the new assigned menu key both point to the same value – GilbertS Sep 04 '20 at 04:51
  • Works just fine! Though it might be obvious to some, I had to reboot for changes to take effect. – Arjuna Deva Jun 22 '21 at 07:23
  • @AlejandroCamus, Really! I don't recall rebooting when I changed the keys – GilbertS Jun 22 '21 at 15:49
  • @GilbertS I am using an Ubuntu virtual machine, would that matter? – Arjuna Deva Jun 23 '21 at 08:40
  • 1
    Do we need restart after changes? – Goran_Ilic_Ilke Sep 20 '21 at 06:38
  • 2
    UPVOTED! Only working solution on the web for 20.04.Well done! – Goran_Ilic_Ilke Sep 20 '21 at 06:47
  • 1
    works amazing (after trying several unreliable workarounds) on Pop!_OS 20.04 to remap "home, end, pgup, pgdn, del" on Keychron K4 v1 – manus Feb 15 '22 at 21:49
  • 1
    This was the only thing that allowed me to remap Search to Caps_Lock on my Chromebook running Ubuntu. – AdvApp Mar 23 '22 at 22:56
  • how do you remap fn? – Bersan Oct 10 '22 at 18:45
  • Usually you can't remap fn - https://askubuntu.com/questions/827925/remapping-the-fn-key – Kyle May 26 '23 at 02:44
  • After days of web surfing, this Sh!t worked on Ubuntu 20.04. Its so simple and minimal in comparison to what I tried so far! Its dumb easy in Windows. But on Ubuntu, we gotta try so many things just to realise it wont work! – RajS Oct 08 '23 at 20:44
19

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.

Oli
  • 293,335
  • 5
    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
  • Not working 20.04 . – Goran_Ilic_Ilke Sep 20 '21 at 06:45
9

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.

David Foerster
  • 36,264
  • 56
  • 94
  • 147
Stuart K. Smith
  • 141
  • 1
  • 4
  • 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
  • Your error message seems clear: You have an error in your ~/.Xmodmap file – Stuart K. Smith Sep 23 '21 at 17:00
5

Go to Startup Applications and make a new entry with the content shown in the following screenshot:

adding new startup application(command)

It works every time.

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.

eexpress
  • 271
3

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.

  • 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
  • The only method that's actually persisted from hibernate, restarts and turning off and on my keyboard. Thanks! – Aspiring Dev Jun 30 '21 at 23:00
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

quaeched
  • 143
  • 2
    Those edits will be overwritten the next time the packages that owns those files get upgraded. – muru Nov 26 '17 at 13:13
  • 1
    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
  • I found that this was the best way to persist the changes too. xmodmap seemed to forget the mapping as soon as I even disconnected my keyboard for whatever reason. This solution isn't perfect but works. We'll see how it goes. @Sanalgorithm's answer above instructs on how to do the remapping alluded to here. – Aspiring Dev Jun 30 '21 at 23:19
1

Graphical Editor

gnome-tweaks > Keyboard & Mouse > Additional Layout Options

Key Maps

Jonathan
  • 3,904
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.

u2n
  • 923
  • 1
  • 9
  • 22
  • Why do you think support for xmodmap will be dropped? – Olaf Dietsche Feb 16 '20 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 '20 at 17:57