Linux Switch Keys with Xmodmap

by|inArticles||2 min read

In some cases it is not enough to detect a keyboard layout in Linux. For instance, using a German Mac Keyboard has switched key mappings for "^" and "<". In this blog post we will show how to switch those keys to it's correct behavior.

To override keys, we need to know which keycode we are searching for. With the command *xmodmap -pke* we can see the current mapping. In our case (by searching for **less greater** and **asciicircum**), those keycodes are *49* and *94*. For now, we know which mappings needs to be switched:

keycode  49 = asciicircum degree grave asciitilde notsign U2033
keycode  94 = less greater less greater bar dead_belowmacron bar dead_belowmacron

To make a temporary change we use the xmodmap command to switch the behavior of the keyboard (by typing the following lines in the terminal):

xmodmap -e "keycode  49 = less greater less greater bar dead_belowmacron bar dead_belowmacron"
xmodmap -e "keycode  94 = asciicircum degree grave asciitilde notsign U2033"

Now, our keyboard should behave as expected with the keys switched. As well, the xmodmap -pke command should now give the correct mapping.

Since our changes are temporarily (it means just valid for the current session), we will loose those settings after system reboot. To prevent this, we can export our keymap file and load it back again each time we log in. Let's export the current settings (with our changes) like this:

xmodmap -pke > ~/.Xmodmap 

This will create a file with the key mappings in our personal user folder. To load this file, we modify the xinitrc file and add the command to load our exported file:

[[ -f ~/.Xmodmap ]] && xmodmap ~/.Xmodmap

By saving the file the system should load the custom Xmodmap file after each system reboot and we have persisted our key assignment changes.

If you have any questions or comments don't hesitate to use the comment area below.

Thank you for reading this far! Let’s connect. You can @ me on X (@debilofant) with comments, or feel free to follow. Please like/share this article so that it reaches others as well.

Related Articles

© Copyright 2024 - ersocon.net - All rights reservedVer. 415