18

After almost every time I type something in caps, I forget to turn off capslock when returning to normal mode. I then try to move the cursor and end up joining a bunch of lines and a lot of other things I don't expect to be happening.

Is it possible to configure vim to turn off capslock when I press escape to enter Normal mode?

If there are solutions that are OS-specific, I am interested in those applicable to Linux.

2

7 Answers 7

8

Responses to this question indicate that it's not likely - Vim doesn't have access to the OS to make that call.

However, you can try changing your behaviour so that Caps Lock isn't required. It's a popular key to map to Ctrl, Esc or Meta.

Vim has the gU operator to uppercase a selection or motion, so perhaps you could type normally and use that afterwards. Also relevant: gu to lowercase and ~ to toggle.

There are also ways to autocomplete, which may reduce your need to type VERY_LONG_MACRO_NAMES.

6

I found a way to turn off caps lock in normal mode automatically (though it works only in Linux).

The solutions that were made with plugins do not take into account that you can enter normal mode in more then one way (that is from insert to normal), for example from command mode.

There is actually no autocommand event that activates upon entering normal mode, so the event I used instead was CursorHold event, which activates when you're not typing in normal mode.

au CursorHold * call TurnOffCaps()
set updatetime=10

You set the update time to a very small number, so it will always activate, before you have a chance to type anything.

function TurnOffCaps()  
    let capsState = matchstr(system('xset -q'), '00: Caps Lock:\s\+\zs\(on\|off\)\ze')
    if capsState == 'on'
        silent! execute ':!xdotool key Caps_Lock'
    endif
endfunction
1
  • I think this is the only real answer to the question. Vim needs to make a call to the OS to turn of CapsLock somehow. Of course how that is done will depend on the OS.
    – Octaviour
    Commented Feb 22, 2017 at 16:00
5

In addition to Avian Y answer, actually there is an autocommand that activates leaving insert mode:

au InsertLeave * call TurnOffCaps()

That was also exactly what I was looking for...

4

I have remapped my capslock to be ctrl and I rarely use capslock anymore. However, for vim if I ever need, you have tpope/vim-capslock as a software temporary capslock for you. If you use this it should be fairly easy to setup an autocmd for the event InsertLeave and disable capslock.

2

Another option is to remap your CapsLock to Esc. This is a mapping I've been using for a couple of years now: CapsLock is on a home row, so it's really easy to use if you touch type. It also promotes proper Vim usage, since getting out of insert mode is much easier (key is closer than Esc).

It's easy to remap: Linux supports this natively, there are small apps for Mac OS X and Windows to accomplish that. And some expensive keyboards even come with a feature that allows remapping keys.

1

In the vim help manual there's pretty nice mapping to make a word all caps in insert mode:

inoremap <c-h> <esc>gUiw`]a

It uses ctrl-h, which is normally just a backspace clone, so you're not really losing anything with this mapping.

Caps lock is a nightmare when you're in normal mode! I hope that helps!

1
1

On my Apple machine, I actually implemented this feature using Karabiner, which worked reasonably well. Unfortunately 10.12 broke Karabiner, and I haven't tried implementing it since.

I had it so using the escape key or the enter key disabled caps lock, if it was enabled before.

So when you're using macOS on 10.11 or below, you can certainly do this (though don't ask me for the settings, I don't have them anymore).

Otherwise I would go with the suggested gU. (Even though I can understand you grievances, about not being able to use the same method you would use in any other text editor)

2
  • that Karabiner-link shows me a bunch of carabiner hooks. What were you originally referencing?
    – julaine
    Commented Feb 14 at 12:39
  • this one? karabiner-elements.pqrs.org
    – julaine
    Commented Feb 14 at 12:40

Your Answer

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

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