How to bind caps lock key to escape key on Windows using AutoHotKey for Vim only


Here’s another Vim tip that I find very handy. When using gVim on Windows, I can bind the escape key to caplock, but only for Vim. This method relies on using AutoHotKey, an excellent tool.

First install AutoHotKey

Once installed, right click the AutoHotKey icon in the system tray and select “Edit This Script”.

A text editor will open, with the default script that executes when AutoHotKey is running.

Add the following code snippet to the end of the script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
classname = ""
keystate = ""</code>
 
*Capslock::
WinGetClass, classname, A
if (classname = "Vim")
{
SetCapsLockState, Off
Send, {ESC}
}
else
{
GetKeyState, keystate, CapsLock, T
if (keystate = "D")
SetCapsLockState, Off
else
SetCapsLockState, On
return
}
return

AutoHotKey is designed to run in the background. When Vim is running, a capslock key press will operate in an identical manner to an escape key press. Even better is the fact that the capslock status for Windows does not change when Vim is in focus.

Enjoy!

  1. No comments yet.
(will not be published)