3

On Xubuntu LTS, I already managed to remap CapsLock to Esc.

However, now I discovered some Vim users are lighting the Scroll Lock LED, Caps Lock LED or even the entire keyboard to indicate that (g)Vim is in the insert mode.

Varmilo Caps Lock with LED

Unfortunately, explanations about how to accomplish this Vim feat (under GNU/Linux) are nowhere to be found.

Can some good Vim soul show me the light?

Fun fact

Bill Joy developed vi from June through October 1977 on a Lear Siegler ADM-3A terminal. On this terminal, the Esc key was at the location now occupied by the Tab key on the IBM PC keyboard. This is somewhat the rationale behind converting the current IBM PC Caps Lock into an Esc key.

ADM-3A keyboard layout

The Lear Siegler ADM-3A terminal

2 Answers 2

6

Caps Lock LED without root privileges

Varmilo Caps Lock with LED

1) Making Caps Lock act like Esc

The full path to below bash script was added to Session and StartupApplication Autostart in Xubuntu LTS.

#!/usr/bin/env bash
sleep 4
xmodmap -e "clear Lock"
xmodmap -e "keysym Caps_Lock = Escape"

2) Edit /usr/share/X11/xkb/compat/ledcaps

$ sudo sed -i 's|\!allowExplicit|allowExplicit|g' /usr/share/X11/xkb/compat/ledcaps

Logging out and in again will allow these X settings to take effect. For more information, see here.

3) Add an augroup to $MYVIMRC

In $MYVIMRC add the following:

" Light the Caps Lock LED when in Insert mode.
" https://vi.stackexchange.com/a/17824/3168
if has("gui_running")
    augroup CapsLockLED
        autocmd!
        autocmd InsertEnter * :silent !xset  led named 'Caps Lock'
        autocmd InsertLeave * :silent !xset -led named 'Caps Lock'
        autocmd VimLeave    * :silent !xset -led named 'Caps Lock'
    augroup END
endif

After sourcing $MYVIMRC or restarting vim, the Caps Lock LED will light whenever vim is in insert mode, by whatever means. Hitting the lit Caps Lock key will return vim to the normal mode.

On my Varmilo VA88M keyboard this LED is built into the Caps Lock key, which results in a nice effect.

4

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.

enter image description here

(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.

1
  • Thank you for taking the effort in writing this somewhat long answer. As for writing to system devices, echo 0 | sudo tee > … is what you are looking for. This is explained here. You are getting there through a sudo detour. Nov 4, 2018 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.