Swapping CapsLock And Escape Keys In The TTY And X11

[home]

The Problem

If you're a vim/vi user like myself you might find it easier to swap the caps lock key and with the escape key as it allows you to not move your hand around the keyboard as much. In the 1970's and 1980's I believe that a lot of keyboards had an escape key which was in the general area of where the caps lock key is now, which is why vi/vim still use them.

Swapping the caps lock key and escape key can be quite tedious so this page will contain as much information I can spare to help you do this across various UNIX systems.


X11

For X11 the process is quite simple and cross platform. All we need is a program called xmodmap. Xmodmap will be installed by default on a lot of operating systems if you have X11 installed, OpenBSD, NetBSD, Linux etc. Some Linux operating systems will not ship xmodmap by default with X11, just check by running it with no arguments. You will also need a program called setxkbmap, this program is less likely to be installed by default, so again make sure you have it by running it with no arguments.

To swap escape and caps lock run the following series of commands whilst in a X11 session:

xmodmap -e "keycode 66 = Escape NoSymbol Escape"
setxkbmap -option caps:swapscape

Now if you hit the escape key it should act as caps lock and if you hit the caps lock key it should act as the escape key, much better.

NOTE:"keycode 66" might change depending on your keyboard layout, I assume every keyboard layout will have the same escape and caps lock placement but you never now.

TTY

Swapping caps lock and escape on the TTY is a much more tedious process, so far I have only managed to do this on NetBSD and OpenBSD.

NetBSD

For NetBSD the process is quite simple, it's just hard to find information on how to do this hence why I am writing this article.
The first step is to echo your current key map into a file in /usr/share/wscons/keymaps/. It's as simple as running this command as root:

wsconsctl map > /usr/share/wscons/keymaps/my_map

This will dump your entire current key map into a file, we do not need all of the text in the file so we will want to delete all un-needed lines. Since all we are aiming to do is swap escape and caps lock just delete all the lines execpt for Caps Lock and Escape and then swap the numbers around so the final product should be somthing like this:

keycode 56 = Cmd_Debugger Escape
keycode 1 = Caps_Lock

This is the what my file looks like. Next we need to tell wscons to load the keymap at boot time, we do this by editing the file /etc/wscons.conf, add the line:

mapfile /usr/share/wscons/keymaps/my_map

Changing my_map to the name of your file and then reboot, now the keys should be swapped around, of course you can use this method to swap any keys that you desire. This method may also work on OpenBSD.