[Tutorial] Replacing Caps Lock with Em Dash and En Dash in Linux

replacing the function of the caps lock button with something useful

Capslock key
The Caps Locks key: A conveniently well-placed button with an outdated and generally useless function.

I wanted to replace my Caps Lock with Em Dash and En Dash? Why? Well, I rarely use Caps Locks. It's a waste of keyboard real estate, and usually the Shift button would suffice.

Note: I am using Linux Mint 18.3 Sylvia, but this tutorial should work for all versions of Linux Mint and Ubuntu.

There are 2 main ways that we can change the Caps Lock function on Mint. One way is to edit/create a completely new XKB layout. The second way is to use xmodmap which changes the keys of an existing layout. I chose the second option because I would have to edit the XKB file for every new keyboard language and keyboard variant.

For me, the first step is to figure out what key the cap locks key is labeled as on the computer. If you are using a US keyboard layout, most likely it is keycode 66, but it's always good to double check.

So open your terminal (you can open it by typing in the shortcut ctrl+alt+t) and type:


Now, now when you press a key, it states what button is being pressed. So when you press your Cap Locks button, it should show something similiar to the following:

KeyRelease event, serial 37, 
synthetic NO, window 0x6200001, 
root 0x146, subw 0x0, time 22321379, 
(-110,231), root:(476,530), state 0x0, 
keycode 66 (keysym 0xaa9, emdash), 
same_screen YES, 
XLookupString gives 3 bytes: (e2 80 94) "—" XFilterEvent returns: False

What we are looking for is the keycode. On my computer, keycode 66 is my cap locks key (This is the caps lock button for most keyboard layouts).

Now we have to make a file to replace the Caps Lock key with the Em-Dash and En-Dash. (You can also use this file to remap any other keys as well.)

We need to make a new file, so open up your favorite terminal text-editor. I like nano. If you still have the xev window open, close it and type:

nano ~/.Xmodmap

Some distros automatically will open this file on startup, so my recommendation is to name it exactly .Xmodmap (case-sensitive). We do not need to be in root because we are making a file in the home folder.

Now, we need to write the following in the file.

clear Lock
keycode 66 = 0xaa9 0xaaa

The clear Lock clears away any caps lock function existing in the keyboard. Now we need to associate the caps lock button with the em dash (—) and en dash (–) symbol. The second line associates the keycode 66 button with em dash (0xaa9) symbol and the shift modifier with the en dash (0xaaa). Once you are done, exit by pressing ctrl+x in nano, type y for yes to save, and then press enter.

Test it out. Type in your terminal:

xmodmap ~/.Xmodmap

If everything was done correctly, your cap locks key should type em dash, and when you shift, it should be an en dash.

If you want to reset the keyboard back to normal, type the following in the terminal:

setxkbmap -layout us

Lastly, we need to make sure that this command will be performed after every reboot. On Mint, you open up the application Preferences>Startup Applications. Press the add button and click on 'custom command'. Add the code xmodmap ~/.Xmodmap into your custom command. (For troubleshooting, try to add the actual home address. Something like xmodmap /home/yourusername/.Xmodmap. Add title and descriptions (whatever you want). Before you save and close, one more thing. For the delay, type in "1". If you leave it at "0", it won't work. At all. I don't know exactly why it doesn't work (though I do have some reasonable guesses as to why).

Save, restart your computer, log in, and see if it works. It should work pretty much instantly.

Blog Comments powered by Disqus.