I’ve configured my keyboard to generate a special key code when you press the Caps Lock key by itself. I use this as a mode switch in Emacs which I have configured somewhat like VIM.

For VIM users the escape key is used to switch between typing and command mode and escape is pressed all the time.

The problem is that it is a long reach to hit the escape key. The Caps Lock key is in a prime location and who needs the Caps Lock key anyway? (You can move it if you want.)

On Ubuntu I use the program xcape to accomplish this remapping. I will show you how to download, install, configure and test xcape.

On the Mac you can use KeyRemap4MacBook and PCKeyboardHack to do the same thing.

Here is the link to xcape: https://github.com/alols/xcape#readme

First install the necessary dependencies. It requires git, gcc, make libx11-dev and libxtst-dev libraries. Open a terminal and type:

sudo apt-get install git gcc make libx11-dev libxtst-dev

Next make a folder for xcape in your home folder:

mkdir xcape
cd xcape

Download the source code for xcape:

git clone https://github.com/alols/xcape.git .

Build the application. This makes a executable called xcape.


LICENSE Makefile README.md xcape xcape.c

To see the usage statement:

./xcape -h
./xcape: invalid option — ‘h’
Usage: ./xcape [-d] [-t timeout_ms] [-e <mapping>]
Runs as a daemon unless -d flag is set

I am using the Ubuntu system settings to turn the Caps Lock into a control key and xcape to generate Alt+R when it (now a control key) is pressed by itself.

Use the system settings to make Caps Lock a control key:

  • Open system settings Keyboard Layout
  • Click Options
  • Click “Ctrl key position”
  • Check “Caps Lock as Ctrl”
  • Close the dialogs

If you are a VIM user and you want Caps Lock by itself to generate an escape, use the defaults and run xcape like:


I want the left control to generate Alt_L+R when pressed on its own, so I run xcape like:

./xcape -e ‘Control_L=Alt_L|R’

You can remap multiple keys at once using the semicolon to separate commands:

xcape -e ‘Shift_L=Escape;Control_L=Control_L|O’

The list of key names is found in the header file X11/keysymdef.h (remove the XK_ prefix). Here is a link to the file:


Run xev to test the keycodes generated when you type a key.


For my case the system generates a Control_L press and release then the Alt_L+R press and release combination.

If you change the keyboard system settings, rerun xcape or it will continue to run with the old settings.

Make sure you only run one xcape process. To see the xcape process or processes:

ps aux | grep xcape
steve 13342 0.0 0.0 11984 688 ? Ssl 09:25 0:00 ./xcape
steve 13467 0.0 0.0 11984 684 ? Ssl 09:44 0:00 ./xcape
steve 13564 0.0 0.0 11984 688 ? Ssl 10:01 0:00 ./xcape -e Control_L Alt_L R

Quit the extra running xcape processes above:

kill 13342
kill 13467

To make xcape run once when you login, add the xcape to your .bashrc file,

emacs ~/.bashrc

add this line to the bottom of the file:

# Generate a Alt+R when the control key is pressed and released by itself.
# Run xcape once.
if [ -z $XCAPE ] ; then
export XCAPE=1
~/xcape/xcape -e ‘Control_L=Alt_L|R’