Note: This article was translated with the assistance of AI. I wrote the original in Chinese. If you can read Chinese, you are welcome to read the original Chinese version for the most authentic and unfiltered expression.

Implemented with AutoHotkey.

Why not use VSCode’s built-in key mapping?

I tried the solution found online, but every time I pressed Esc to return, it also toggled the Caps Lock state, which felt unnatural.

Install AutoHotkey

AutoHotkey

Since the script is written for version 2.0, choose Download v2.0 when downloading.

Write the script

Create a vscode-vim.ahk script in a suitable location. If you want it to run at startup, you can write it directly under shell:startup, or create a shortcut to the script in the startup directory.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#Requires AutoHotkey v2.0

#HotIf WinActive("ahk_exe Code.exe")

; Map Caps Lock key to Esc
CapsLock:: {
Send("{Esc}")
}

; Map Esc key to Caps Lock
Esc:: {
if (GetKeyState("CapsLock", "T")) {
SetCapsLockState("Off")
} else {
SetCapsLockState("On")
}
}

#HotIf

This script only takes effect in VSCode, swapping the Caps Lock and Esc keys. To enable the script, just double-click to run it.