Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to remap the CapsLock key system wide such that when the CapsLock button is pressed by itself it acts as the escape key and when pressed with another key it acts as the control key?

share|improve this question
This seems like it would be very difficult. For example, it couldn't generate key down events, because you don't know which key was just pressed. If it's released without another key being pressed, then it was Escape, otherwise it was Control. – cjm Feb 17 '12 at 17:12
up vote 13 down vote accepted

I have found a very good solution called xcape. From the README:

xcape runs as a daemon and intercepts the Control key. If the Control key is pressed and released on its own, it will generate an Escape key event.

This makes more sense if you have remapped your Caps Lock key to Control. Future versions of this program might do that mapping for you, but for now this is something that you have to do yourself.

share|improve this answer
I made a hacky script to do this: github.com/cmatheson/super-caps – cam Apr 5 '13 at 5:13

I mapped Caps to Escape so that I would be more comfortable typing in Vim. I used xmodmap, and put this in ~/.Xmodmap.

clear Lock
keycode 0x42 = Escape

To apply: xmodmap ~/.Xmodmap. You can put this in a file such as ~/.bash_profile (I put in in ~/.xinitrc, because I don't use a login manager.

To map CapsLock + [another Key], consider using xbindkeys.

share|improve this answer

A Linux-only solution for remapping keys system-wide is to write a simple udev rule. It won't depend on X, will work on any terminal or app and is portable between any recent Linux installs by simply copying two files.

You need to write 2 files: one containing the udev rule which tells the kernel to remap the keys and one containing the actual mappings you need.

Using this method I've remapped my whole keyboard, maybe the code is useful as inspiration http://git.adirat.com/carpalx-keyboard

share|improve this answer
the link is dead, could you please update it? thanks! – qed May 13 '13 at 15:31
I updated it, sorry for the delay – gimmesudo Jun 12 '13 at 1:10
Bitbucket says "You do not have access to this repository." Can you make it public again? Thanks! – Tianyi Cui Jul 5 '13 at 0:03
@TianyiCui check the link now – gimmesudo Jul 5 '13 at 5:48
The link is dead or blocked for me. Any chance you could put the actual files up in your answer? – labyrinth Sep 14 '15 at 22:15

I ran across a differently worded question on AskUbuntu that had a good answer, but I had to modify it slightly to work on my systems. https://askubuntu.com/questions/177824/remapping-caps-lock-to-control-and-escape-not-the-usual-way/750535#750535

Per Louis and Sergiy's comments over there I came up with this solution that is working for me on Ubuntu 16.04 (Xenial) and I'll be bringing it with me to the other Linux systems I use.

I installed xcape and put the following lines of code into my .profile file in my home directory.

setxkbmap -option 'caps:ctrl_modifier' xcape -e '#66=Escape'


I've used gnome-tweak-tool to map Caps Lock to Ctrl as there is no Keyboard layout in System Settings on Ubuntu 14.04. Then xcape -e 'Control_L=Escape' didn't work, but after using xcape -d I've discovered that Caps Lock generates keycode 66 and remapped it respectively: xcape -e '#66=Escape'. – Sergiy Byelozyorov Sep 4 '14 at 10:08


As per this blog post, it is possible to do this with xcape alone: setxkbmap -option 'caps:ctrl_modifier' xcape -e 'Caps_Lock=Escape' setxkbmap -option 'caps:ctrl_modifier' xcape -e 'Caps_Lock=Escape;Control_L=Escape;Control_R=Escape' – Louis Simoneau Aug 4 '15 at 10:06

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.