1

I am trying to remap caps lock to escape on the keyboard. I tried the following solution found on AskUbuntu without success:

$ xmodmap -e "keycode 9 = Caps_Lock NoSymbol Caps_Lock"
$ xmodmap -e "keycode 66 = Escape NoSymbol Escape"
$ setxkbmap -option caps:swapescape

what shall I do?

4
  • @Quasímodo . I am sorry what you mean with "Wayland perchance?"
    – ecjb
    Jun 25, 2022 at 15:59
  • ok thank you for your comment @Quasímodo. Do you have a hint about how to see on which one I am?
    – ecjb
    Jun 25, 2022 at 16:47
  • Ok @Quasímodo. I typed the command echo $XDG_SESSION_TYPE and indeed I am on Wayland. Could give me hints about what I shall do?
    – ecjb
    Jun 25, 2022 at 17:07
  • 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 7, 2023 at 12:57

2 Answers 2

3

To do this with xkb, I prefer to do the following:

  1. Create a directory to store my custom keymap, so changes can be reapplied if a reinstall is needed. I chose to create the directory ~/.settings/keyboard/, then create a file (I chose to call mine uc, us with a c for custom) with the following content:
default partial alphanumeric_keys
xkb_symbols "uc" { 
    name[Group1]="uc";
    include "us(basic)"
    key <ESC> { [Caps_Lock] };
    key <CAPS> { [Escape] };
};

You can then create a symlink to this file like so:

cd /usr/share/X11/xkb/symbols/
sudo ln -s ~/.settings/keyboard/uc uc

Make sure that the link is to the right file using the command

ls -l /usr/share/X11/xkb/symbols/uc

Doing this is enough to then use the command setxkbmap uc (Which I recommend doing, to ensure that everything goes as expected) which will then switch those keys as long as the X server is running. (Note: in wayland, for me this did not actually set the keymap, but I had to do the next step, and then go to the keyboard menu and set English (Custom) as the top keyboard layout, and rebooting. If multiple layouts are selected in the keyboard menu, I have also had to click on the en appearing in the top right corner, selecting English (Custom), and then select Show Keyboard Layout, then the layout is loaded).

  1. To make this change permanent, (also to make this new keyboard layout available in the keyboard menu, this works in cinnamon anyway), also copy the file
sudo cp /usr/share/X11/xkb/rules/evdev.xml ~/.settings/keyboard/

Then edit the file to contain your layout, this will look like the following, in the <layoutList> section, insert the following:

...
<layoutList> 
  <layout>
    <configItem>
      <name>uc</name>
      <shortDesciption>uc</shortDescription>
      <description>English (Custom)</description>
    </configItem>
  </layout>
...
...
</layoutList>

(Note the <layoutList> and </layoutList> already exist in the file, we are just adding a new layout)

Then, the following commands will enable you to select this keyboard layout from the keyboard menu:

cd /usr/share/X11/xkb/rules
sudo mv evdev.xml evdev.xml.original
sudo ln -s ~/.settings/keyboard/evdev.xml evdev.xml

Then, opening up your keyboard settings menu, you will be able to see a layout named English (Custom) that contains the desired keyboard layout. If you set this to be your keyboard layout from this menu, the changes will be persistent every time you log in.

Hopefully this helps!

5
  • Dear @BradenCarlson. Thanks a lot for your answer! I could not try it yet on my raspberry pi but I will as soon as I find the time. As pointed out in the comment below the question, the problem seems to be related to wayland. Do you think that your answer will work on wayland?
    – ecjb
    Jul 12, 2022 at 6:51
  • Since you are able to run commands such as xmodmap I assume so. According to this answer, wayland does use xkb, so everything should go smoothly! If not please leave another comment and I will do more research and get back to you! Jul 12, 2022 at 14:38
  • Thank you @BradenCarlson, I coud not test the answer yet but upvoted your answer in the meantime
    – ecjb
    Jul 12, 2022 at 14:48
  • @ecjb I have tested this on Gnome with wayland, and I found a few typos in my keymap file, but they have been fixed in the answer, and I had no problems setting this as my keyboard layout, although I did have to reboot for it to take effect. Let me know how it goes for you! Jul 12, 2022 at 16:33
  • 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 7, 2023 at 12:57
2

Try installing https://github.com/sezanzeb/input-remapper (on ubuntu, just do sudo apt install input-remapper. You can remap keys there.

1
  • this is the right answer
    – Allexj
    Jan 7, 2023 at 12:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .