Remapping Caps Lock in Ubuntu

on linux


I been using this great drop-down terminal called Guake for the past couple of years. I conveniently set the open hot key to F1, so I can easily open guake anytime. This worked for a while until I started playing video games on Ubuntu, the F1 key is often used in real-time strategy games, so now I had to pick a new hot key.

I started scanning my keyboard for the most uncommonly-used convenient key. And I found it, the caps lock key. I always heard of people remapping it to escape or a ctrl key, and now is the time to try it.

From the Guake preference you can't actually map caps lock to the open hot key, so I decided to make the hot key F12 and remap caps lock to F12.

Did some searching online and found this to be the simpliest solution:

xmodmap -e "keycode 66 = F12" -e 'clear Lock'

This will map caps lock to F12 and make the key no longer act like caps lock. Except this doesn't persist on re-logins, suspensions and it's deprecated, so best using something else.

Next up setxkbmap, it's apparently the new way of remapping keys. Running the help command I see this.

For Linux (including Ubuntu):

setxkbmap -option <OPTION>
Where <OPTION> can be for example of:

caps:none – Disables Caps Lock.
caps:super – Caps Lock becomes an additional Super (aka Win key).
caps:ctrl_modifier – Caps Lock becomes an additional Ctrl.
caps:numlock – Caps Lock becomes an additional Num Lock.
caps:escape – Caps Lock becomes an additional Escape.
caps:backspace – Caps Lock becomes an additional Backspace.
caps:swapescape – Caps Lock becomes Escape, and Escape becomes Caps Lock
Note: Xmodmap is deprecated in favor of setxkbmap.

Awesome, but how do I remap caps lock to F12?

From what I could gather, you're supposed to remap keys by editing this file /usr/share/X11/xkb/symbols/pc. There are symbol files for many different keyboard layouts but pc is the root-level keyboard and is loaded first. The format is quite simple, give it a key code and then set what it should do.

The next step is to find the keycodes for F12 and caps lock.

Type in xev inside your terminal, when you hit enter, xev will launch. xev takes all of your input and pops info about the key into the command line. I recommend bringing your mouse over the exit key of the xev window, hitting caps lock (or whatever key you want to find out about), and immediately exiting xev.

posts/remap_caps_lock_in_ubuntu/xev_window.png Press your keys inside this window

posts/remap_caps_lock_in_ubuntu/xev_terminal.png You will see your key presses in the terminal

KeyRelease event, serial 37, synthetic NO, window 0x5000001,
    root 0x2c9, subw 0x0, time 12007409, (14,5), root:(885,468),
    state 0x0, keycode 96 (keysym 0xffc9, F12), same_screen YES,
    XKeysymToKeycode returns keycode: 66
    XLookupString gives 0 bytes: 
    XFilterEvent returns: False

The key name, not surprisingly, is F12, so now to put it all together. Copy and paste this to /usr/share/X11/xkb/symbols/pc:

key <CAPS> { [ F12 ] };
key <NMLK> { [ Caps_Lock ] };

Just incase I ever want to use caps lock again, I remapped number lock to caps lock.

Delete the cached server-* files in /var/lib/xkb/ and restart your computer.