Remapping Caps Lock to Control and Escape (not the usual way)
I have this setup on my Mac and I had tried to find a way for Ubuntu without luck.
Well, it's now working. Thanks tungd for ponting me to xcape
. What I have done is really simple and straightforward.
-
Under System Preferences → Keyboard Layout → Options... → Ctrl key position, I checked Caps Lock as Ctrl.
-
In a terminal run:
xcape -e 'Control_L=Escape'
Done, Caps Lock works as Ctrl when used like a modifier, and as Esc when pressed alone (there's a slight delay of a few hundred milliseconds, as noted on the xcape
README file, so it's still not as optimal as the Mac solution which uses KeyRemap4MacBook.)
Install xcape
sudo apt-get install xcape
Add the two lines in your ~/.profile
/usr/bin/setxkbmap -option 'caps:ctrl_modifier'⏎
/usr/bin/xcape -e 'Caps_Lock=Escape' -t 100
Notice the part -t 100
, if you don't set it, you may notice a delay in Esc if you use vim.
I'm looking for this too. As of a week ago I found my self a partial solution using xmodmap
:
add Control = Caps_Lock
remove Lock = Caps_Lock
keysym Caps_Lock = Escape
This is of course not working perfectly, it effectively send both Escape and Ctrl at the same time when I press CapsLock, but I was happy with it for a while.
Finally I found this little utility https://github.com/alols/xcape. So now I can simply remap CapsLock to Ctrl and let xcape do its job.
EDIT: Fixed this for most smart (newer) display managers.
Per Louis and Sergiy's comments I have come up with this solution that is working for me on Ubuntu 16.04 (Xenial) with LightDM and Unity.
I put the following line of code into my .xprofile
file in my home directory.
setxkbmap -option 'caps:ctrl_modifier' && xcape -e 'Caps_Lock=Escape' &
The trailing & puts the xcape command into the background so that it doesn't block your session. The setxkbmap makes its change immediately.
Sergiy:
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
Louis:
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
I've built a tool in C specially for this purpose that overcome many of the issues with the xcape/xmodmap solution:
- https://github.com/oblitum/caps2esc