Unfortunately, explanations about how to accomplish this Vim feat (under GNU/Linux) are nowhere to be found.
You can craft your own solution, using building blocks.
Vim can execute external commands. Try :!ls
if you are not familiar with this feature.
Vim can also map keys to other keys, or key sequences. If you are not familiar with this feature, try:
:noremap i iLOL
After having issued this mapping, every time you go into insert mode, Vim will insert LOL, as if you typed it.
The last building block you need is a command to control the lights on your keyboard. The command you need might differ from system to system. My keyboard does not have a caps lock light, for instance (Lenovo T440s). Instead I used the keyboard backlight. I recommend you create your own command, say, in your home directory, ~/light_on
and ~/light_off
. (Feel free to choose another path.)
Don't forget to chmod +x lights_o*
.
Contents of lights_on
:
#!/bin/bash
echo 2 | tee > /sys/class/leds/tpacpi\:\:kbd_backlight/brightness
Contents of lights_off
:
#!/bin/bash
echo 0 | tee > /sys/class/leds/tpacpi\:\:kbd_backlight/brightness
These work for my computer. It may or may not work on yours. You should edit these files to your computer's requirements. Make sure you look into the directory /sys/class/leds/
. There are other lights as well. Caution: In that directory echo 0 > file
does not work. Instead you should use echo 0 | tee > file
. I don't know why.
Now, you understandably need superuser rights to control the lights in this manner. In order to run these scripts, you need to sudo ./lights_on
and then type the sudo password. This is inconvenient. (xset
might not need sudo rights, see below.)
You can authorize users to run certain privileged commands without needing a password by editing /etc/sudoers
. I did this by following https://unix.stackexchange.com/questions/215412/allow-certain-guests-to-execute-certain-commands. I added lines 18 and 28. See image.

(Caution: I had to write line 28 after line 27 to get it working, unlike the instructions in the link.)
If you cannot get the echo | tee
trick working, here's another suggestion: I have also come across xset led on
and xset led off
. Try if these work for you, or try to find out more on xset
. (I personally could not get xset
to control my keyboard lights.)
At this point you should be able to control your keyboard lights by sudo ~/lights_on
and sudo ~/lights_off
, without a sudo password.
Now you could do :nnoremap i :!sudo ~/light_on<cr><cr>i
. But there is more than one way to enter insert mode - i
I
a
A
o
O
and maybe more. So instead you should use autocmd
:
:autocmd! InsertEnter * execute ('!sudo ~/light_on &')
And of course, upon leaving the insert mode, the lights should go off:
:autocmd! InsertLeave * execute ('!sudo ~/light_off &')
You could add this to your vimrc once you are satisfied with the behavior.