11

When editing code in Vim, I will often use caps lock when writing stuff in ALL CAPS. However, I often forget to turn off caps lock when I'm done with the capitalized portion. This causes no end of pain (since, ie, j moves down, but J joins the current line with the line below).

Usually, I want to turn off caps lock when I exit insert mode. How can I add something to my .vimrc so that it will either turn caps lock off when exiting insert mode? Alternately, how could I add something to my .vimrc so that it will alert me (status line? beep? any alert is fine) if I have caps lock on when exiting insert mode?

Thanks!

Sam King
  • 2,068
  • 18
  • 29
  • You can use `autocmd InsertLeave` to perform an action when leaving insert mode but I don't think there's a way to turn caps on/off from within vim since it's a modifier key (you might be able to do it in an OS specific way though). – Benj May 10 '12 at 08:34
  • 3
    Why wouldn't you take the habit of typing normally and *then* change the case of your text? – romainl May 10 '12 at 08:38
  • 1
    [This](http://superuser.com/questions/399903/how-do-i-represent-the-capslock-key-for-vim-key-mappings) will help. – Pavan Manjunath May 10 '12 at 09:05
  • i would too recommend using for example `nnoremap gUiw` and `inoremap gUiwea` to uppercase single words; additionally i use autocomplpop-plugin so any word i already typed is suggested in the way i typed it. – epsilonhalbe May 10 '12 at 10:38
  • 1
    @epsilonhalbe (+romainl): Yeah, I have something similar, but I still find using caps lock more convenient and faster aside from the issue described here. – Sam King May 11 '12 at 07:07
  • @Pavan Manjunath: The link you provided seems to suggest remapping the caps lock key. Am I correct in assuming that that would prevent me from being able to use caps lock in other applications unmodified? I'm hoping to avoid keeping a different mental map of how to capitalize things in different contexts if that is avoidable. – Sam King May 11 '12 at 07:12
  • I wanted this sort of functionality too until I realized how easy stuff it is to uppercase in Vim. My answer to the problem? Just don't use caps-lock and use things like `gU` + movement or `vi"U` (to uppercase stuff inside quotes) to get the job done after typing it in lowercase. – Jeff B Aug 20 '15 at 19:12
  • Here's [one possible solution](https://www.reddit.com/r/vim/comments/bzbv98/comment/eqs2lzt/?utm_source=share&utm_medium=web2x&context=3) utilizing `xset` and `xdotool` if you are using a desktop system running X. But I think it will not work when we're accessing a remote machine through SSH? – M Imam Pratama Oct 31 '21 at 15:35

1 Answers1

7

Like all other applications, Vim / GVIM sits atop the terminal / window manager abstractions, and therefore has no direct access to the underlying raw keyboard functions. So getting Caps Lock information would involve messy and platform-specific hacks.

Instead, I would advise you to use Vim features to "emulate" Caps Lock within Vim, as this doesn't have the problems you've mentioned. Have a look at the article Insert-mode only Caps Lock, which explains this in detail.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • Thanks! That article suggests using an alternate keymapping (ctrl+^) to invoke the caps lock emulation. Is there a way to do this using the caps lock key itself (without remapping it to another key on the OS level, which would presumably pprevent it from working in other applications)? – Sam King May 11 '12 at 07:05
  • @Sam: On Windows, you could selectively remap the Caps Lock key only when Vim is active via an AutoHotkey script. But many die-hard Vim users will argue that this key is far too valuable for the original use, and rather map this to either Ctrl or Esc (I do the latter). There's another article on the Vim Tips Wiki on how to do that. – Ingo Karkat May 11 '12 at 09:49
  • That makes sense -- I've done stuff like it using AHK in the past. Anything similar on Linux that you know of? – Sam King May 12 '12 at 04:08
  • is it possible to change the Ctrl+^ combination to active caps lock in insert mode? For me "Shift+Shift" would make more sense – asa May 12 '21 at 19:43
  • @AfonsoSchulzAlbrecht: You can remap it: `:inoremap `. "Shift+Shift" won't work for the same reason given in my answer for Caps Lock: It needs to be something that is "seen" by Vim, and just modifier keys (Shift, Ctrl, etc.) won't reach Vim at all. – Ingo Karkat May 13 '21 at 19:17