How to remap your keys in ubuntu

Hassan Zaid
4 min readAug 17, 2020

Having broken keys on your keyboard sucks. Especially when you love typing fast . Imagine having one broken key which forces you to use on screen keyboard all the time you want to send messages to your friends or whenever you want to code . Using onscreen keyboards can get really irritating and it also reduces your efficiency. There are keys that we use regularly and there are others that we barely use or there might be two keys doing the same thing and you only need one of them; Right and Left ALT..Right and Left Control.

Let’s say your m key is broken and you don’t think it’s economical replacing a whole keyboard because of that one key . You can map key m with the Right Alt to solve this problem . Remapping the keys means changing the input behavior of your keyboard and making it work as we want, not as it was intended . In this tutorial we will learn how to do that in Ubuntu using “xmodmap” .

xmodmap -pk

Type the above command on your terminal.It is used to find the keyCode(number assigned to key) and the Keysum(name of the key) for your desired keys to swap.

From the output we will be able to get the KeyCode and the Keysum of our desired keys. In my case if you scroll down the terminal you’ll find out that
m has a keycode value of 58 while Right Alt has a keycode value of 108.

Create a shell script and put the commands for mapping the keys in it . We will be using nano text editor and the script will have a “.sh” extension. Type nano on your terminal window to open the text editor

The command will open the below window

So, the list of commands goes as follows:

#!/bin/bashxmodmap -e “keycode 58 = Alt_R” && xmodmap -e “keycode 108 = m”

When you are done with the commands that are mentioned above then you press CTRL + X to save the script and exit. After that the system will ask you for confirmation and then it will ask you to write a name for the file. I will be saving the file as “key.sh”.
After you do this the editor will exit and save your script
Till here you will have successfully created a simple script, now the script has default permission of rw — -r- -r (the first flag is for the current user, the second one is for user groups and the third one is for others). The permission format consists of binary numbers that represent permissions. The basic permissions are 4 2 1.

Read- read permission is assigned to 4
Write- write permission is assigned to 2
Execute- execute permission is assigned to 1

So, to execute the script you saved you need to change its permission to 7 7 4. The concept of this is that if you want to give an rwx (read, write, execute) you will have to add 4(read)+2(write)+1(execute) that will sum up to 7. So the permission of that script needs to be 774 for the user to execute it.
Now, to change the permission of the script you saved, you will have to write the following command in the terminal:


sudo chmod 774 key.sh

After you press enter it will ask you for the current user’s password, after entering the password it will change the permissions for the file.

Now, to run the script you just have to type “./nameofscript”.


./key.sh

Now, if you followed all the steps carefully and as mentioned above then you will have created a script and executed it successfully.To make it easier to automate the scripts, you create an alias for the script you made so that you don’t have to type ./key.sh every time you want to execute it .

First of all, you need to make a file called .bash_aliases in your home folder. You will have to enter the following command in the terminal:

touch .bash_aliases

Once it is created, open the file by using the command:

nano .bash_aliases

After you enter the above command nano text editor will open then you will have to type

 alias key=”.key.sh”

In this way, an alias for the script you made will be saved and this alias saves the user the trouble of writing “./key.sh” every time you need to execute the script.

So now after creating the alias you just need to write “key” in the terminal and the script will be execute

--

--