2

A lot of people have asked and written about how to map Caps Lock to Esc or Control, but I'd like to use the Caps Lock key as a toggle between insert mode and normal mode, and I haven't found anything that addresses how to do this.

(I'd then like to make ShiftCaps Lock do what a Caps Lock alone normally does, for those rare times when you need Caps Lock, like when typing long CONSTANT names. But that's really a separate question that I'll get to later if there's actually a way of accomplishing the first part. All of this may very well be impossible.)

I'm primarily using Windows at work these days, so that's what I care about most at the moment. (Since this is likely to involve hacks of some sort that are Windows-only, I'll ask a separate question for OS X, which I also use.)

Community
  • 1
  • 1
iconoclast
  • 21,213
  • 15
  • 102
  • 138
  • Do you really mean "command mode" as in "go to the command line" like you get when you press `:`, or do you mean "normal mode"? – dash-tom-bang Nov 02 '12 at 18:29
  • I guess I mean "normal mode" (which is a pretty weird name, considering that in that mode your keys issue commands, which is not exactly normal for keys on a keyboard, especially in a text editor). Fixing question to make it more clear. – iconoclast Nov 05 '12 at 17:14
  • 'normal' is intended to mean "this is the mode that Vim is usually in." "Command mode" means something very specific and is quite different from normal mode. (FWIW I use CapsLock as Escape but it's easy for me because my keyboard supports remapping at the hardware level.) – dash-tom-bang Nov 05 '12 at 22:39
  • Yeah, I know, but I still think it's a very counter-intuitive name. – iconoclast Nov 05 '12 at 23:40

2 Answers2

2

Your request is unusual, and I would recommend against deviating from the default mode switching that much, but you can try this:

  1. First, using operating-system specific methods (as outlined in the links you've mentioned) you need to remap your Caps Lock key to another (unused, e.g. F12) key, because the special Caps Lock cannot be remapped within Vim.
  2. Then, define two Vim mappings for the remapped key which change modes.

Here's a simple example:

:nnoremap <F12> a
:inoremap <F12> <Esc>

Then, you can use the Caps Lock key (remapped to F12) to toggle between insert and command mode.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • Do you recommend against it because (1) you think it will result in bad usability, or (2) because it will be hard to accomplish with Vim? – iconoclast Nov 05 '12 at 17:19
  • @iconoclast: I wasn't asked but I would recommend against it because it is not idiomatic vim to enter insert mode in just one way. `i` or `a` or `cc`... there are so many ways to get into insert mode and none is particularly often used. – mike3996 Nov 05 '12 at 17:55
  • When you get used to your mapping scheme, you'll struggle on systems that don't have it (and the Caps Lock remapping is quite difficult to drag over to new or foreign systems). Also, you'll shut the door to using vi-modes in other applications (like Pentadactyl for Firefox), or have to re-implement your mapping in each of them (potentially struggling with lesser remap capabilities in them). – Ingo Karkat Nov 05 '12 at 18:17
  • @progo: you might have given the *only* reason I might not do this--if the only way to implement this forces me to make Caps Lock the ***only*** way to enter insert mode, then I would agree: that would be bad. Hopefully it will be possible to use all the normal ways of entering Insert mode as well... but I haven't gotten a chance to test this yet... I'm not concerned with having to use Esc on other systems, though. – iconoclast Nov 05 '12 at 18:41
  • 1
    @iconoclast: it'll quite likely work well, but you'll get little benefit over just mapping Caps lock to Escape. And to think of it, both `i` and `a` are slightly more accessible than caps lock by itself in entering insert mode. – mike3996 Nov 05 '12 at 18:44
  • @progo: good points. My ideal would be the `i` and `a` would also change the mode *and* change the light on the *Caps Lock* key (or elsewhere on the keyboard) so that *Caps Lock* is a toggle, but not required. Of course this breaks down with keyboards that physically lock in position when *Caps Lock* is pressed, but I don't think any of my computers have one of those, and I don't think I've seen one like that for perhaps decades... – iconoclast Nov 06 '12 at 15:26
  • When I remapped my Caps Lock key to Esc on Windows Vista / 7 via a registry remapping, the indicator light gratefully stopped working. – Ingo Karkat Nov 06 '12 at 15:29
1

I don't think you can do this natively with Vim, but It looks like you could probably do this using AutoHotKey. I'm not an Autohotkey expert, but here's what I slapped together:

Taken from the example here:

This script uses Capslock key to give a hotkey 2 different outputs. I'm not sure if you need to use +; instead of : in the second part to switch to command mode.

n=0
Capslock::
On := 1
If On=True {
Send i
On := False
} Else {
Send :
On := True
}
Return

The above script is untested, but should switch between sending i (for insert) or : (to insert command mode). Unfortunately this won't keep track of the current state VIM is in. You'd have to learn my AHK scripting for that (how to use variables, so when in insert and you hit keys like '?', '/', 'esc', ':', etc it would know to variable used to store the VIM mode in AHK as well as send the real code).

References:

http://www.autohotkey.com/board/topic/68710-autohotkey-toggle-state-function/ http://www.autohotkey.com/docs/commands/SetNumScrollCapsLockState.htm http://www.autohotkey.com/board/topic/42379-toggle-key/

For your second part it looks like it could be done with something like:

http://www.autohotkey.com/board/topic/51215-completely-disable-capslock/ http://www.autohotkey.com/board/topic/61-use-capslock-as-a-toggle/ http://www.autohotkey.com/board/topic/68710-autohotkey-toggle-state-function/

Not totally sure but you made to do the registry edit for VIM to recognize the Capslock change correctly:

http://www.autohotkey.com/docs/misc/Remap.htm#

James Affleck
  • 293
  • 1
  • 5
  • 10
  • Yeah, AutoHotkey is one of those _operating-system specific methods_ I hinted at in my answer. However, I think trying to duplicate the complex Vim state chart in order to track the current mode in the script is bound for failure (just think of arbitrary mappings). However, the mode could be included in Vim's `'titlestring'`, and the window title could then be captured and parsed by AutoHotkey. – Ingo Karkat Nov 05 '12 at 20:02