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?
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
clear Lock keycode 0x42 = Escape
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
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
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
installed otherwise your system could hang when you attempt to login because all startup scripts are expected to exit cleanly.
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