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?

  • 6
    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, 2012 at 17:12

6 Answers 6


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.


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.


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 https://github.com/10ne1/carpalx-keyboard

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

I've built a tool in C specially for this purpose that overcome many of the issues with the xcape/xmodmap solution:


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.

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.


Because of the various display managers (gdm/kdm/sddm/etc) behaving in strange ways and resetting the setxbmap after suspend/resume or lock/unlock, I've discovered how to set the options in a way that they inherit it and don't reset it all the time. Because I suspend and resume a lot I needed to make sure I didn't get lots of copies of xcape running which is solved in the new script below.

First modify the keyboard config with sudo gedit /etc/default/keyboard and set XKBOPTIONS="ctrl:nocaps" and then in your ~/.xprofile put the single line below.

ps aux | grep -i '[x]cape' && killall -9 xcape; hash xcape 2>&1 >/dev/null && xcape -e 'Control_L=Escape' -t 100 &

Hopefully the various sections of the script save you some time/trouble and prevent stuck logins. First it checks if there is already an instance running and kills it, since the only thing xcape does is map Ctrl to Esc, this is safe, then the hash portion ensures that you have xcape installed otherwise your system could hang when you attempt to login because all startup scripts are expected to exit cleanly.

Old way:

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


There are other solutions being confined to vim by running shell command in the background, but all of the ones that i've encountered have caveats in one way or another.
i've settled on making the change permanent by changing it system wide , so as for ubuntu users you can use the prepackaged caps2esc from a PPA , then adding the following configurations specified with this answer, so you'll end up with the the caps lock and escape button swapped system wide.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .