Transforming the most useless key ever in the most useful one. For vi/Vim/NeoVim addicts at least.
The project now lives on https://gitlab.com/interception/linux/plugins/caps2esc as an Interception Tools plugin. This repository is now frozen.
Because CAPSLOCK is just "right there" and making it CTRL when key-chording and ESC when pressed alone is quite handy, specially in vi.
gcc caps2esc.c -o caps2esc -I/usr/include/libevdev-1.0 -levdev -ludev
The following daemonized sample execution increases the application priority (since it'll be responsible for a vital input device, just to make sure it stays responsible):
sudo nice -n -20 ./caps2esc >caps2esc.log 2>caps2esc.err &
I'm maintaining an Archlinux package on AUR:
It wraps the executable in a systemd service that can be easily started, stopped and enabled to execute on boot.
Executing caps2esc
without parameters (with the necessary privileges to access
input devices) will make it monitor any devices connected (or that gets
connected) that produces CAPSLOCK or ESC events.
Upon detection it will fork and exec itself now passing the path of the detected device as its first parameter. This child instance is then responsible for producing an uinput clone of such device and doing the programmatic keymapping of such device until it disconnects, at which time it ends its execution.
As always, there's always a caveat:
I can't recall when I started using CAPSLOCK as both ESC and CTRL but it has been quite some time already. It started when I was on OS X where it was quite easy to achieve using the Karabiner, which already provides an option to turn CTRL into CTRL/ESC (which can be coupled with OS X system settings that turn CAPSLOCK into CTRL).
Moving on, permanently making Linux my home, I searched and tweaked a similar solution based on xmodmap and xcape:
It's a simple solution but with many annoying drawbacks I couldn't stand in the end:
Meanwhile on Windows land, I had a definitive solution based on my Interception library that always works perfectly, no hiccups.
It made me envy enough, so I ported the Windows Interception caps2esc sample to Linux based upon evdev, udev and uinput.
Copyright © 2016 Francisco Lopes da Silva.