I have a keyboard on which the "P" key does not work but I can't see the point of chucking it away for one defect. I therefore want to map one of the working keys to "P". This has proved a mighty, almost circular, challenge. As is typical in Linux as well, there are many solutions, all of which kind of work but not really. Ideally I want the keys swapped anytime I boot, be it to the terminal (CLI) or X/Desktop (GUI).

The "circular" problem is that's a right mission to enter the character "P" in the fixes below if you don't actually have a character P.

How best to map a working key (like section/degrees) to a non-working key like "P"?

  1. Determine the "key code" of your working key (in my case the § called "section" or "silcrow"):


Run this then press your working key. It should show something like:

keycode 41 press
keycode 41 release

So "41" is the keycode of my working key.

  1. Map that physical keycode to your missing key.

    keycode 41 = p | sudo loadkeys

Now, if you can't actually type your missing key (e.g. "p") it get's... interesting...

The best solution I've found is to echo $'\x70' into a file (bash script) and then go in there and use it in a script command. Let's say you want to add the mapping to your boot routine - you would add it to the file ~/.bashrc.

echo keycode 41 = p >> ~/.bashrc

Now you need to edit .bashrc and change the last line from:

echo keycode 41 = p


echo keycode 41 = p | sudo loadkeys

Now, if you reboot it should map key 41 (section) to "P".

Note 1: The following solution:

xmodmap -e "keycode 49 = P"

is a good method BUT it only works if you booted X (Desktop). It's nice because it doesn't require the dreaded sudo so you can add it to your startup ~/.bashrc.

Note 2: Using Ctrl+Shift+u and entering 0050 (the ASCII code for "P") did not work.

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.