Overview
TheVim.handleKey() function allows you to programmatically send key presses to the Vim mode handler, as if the user had pressed those keys. This is useful for automation, custom commands, or integrating Vim behavior with other features.
Signature
Parameters
The CodeMirror editor instance. You can get this from the EditorView using
getCM(view).The key to send to the Vim handler. This can be:
- A single character:
"j","k","a", etc. - A special key wrapped in angle brackets:
"<Esc>","<CR>","<Space>" - A key with modifiers:
"<C-w>","<C-o>","<S-a>"
<Esc>- Escape<CR>- Enter/Return<Space>- Space<BS>- Backspace<Del>- Delete<C-x>- Control+x<S-x>- Shift+x<A-x>- Alt+x (Meta on Mac)
The origin of the key press. Optional parameter that indicates where the key came from:
"user"- Simulates a user keypress"mapping"- Key came from a mapping
"user".Return Value
Returns:
trueif the key was handled by Vimfalseif the key was not handledundefinedif the key resulted in no action
Examples
Exit Insert Mode
Programmatically exit insert mode:Execute a Command Sequence
Execute a sequence of Vim commands:Custom Button Integration
Integrate Vim commands with UI buttons:Execute Macro Programmatically
Simulate recording and playing a macro:Keyboard Shortcut Handler
Integrate Vim with custom keyboard shortcuts:Automation Script
Automate complex editing tasks:Testing Helper
Use in tests to simulate user input:Special Keys Reference
| Key Name | Description |
|---|---|
<Esc> | Escape key |
<CR> | Enter/Return |
<Space> | Space bar |
<BS> | Backspace |
<Del> | Delete |
<Tab> | Tab |
<Up>, <Down>, <Left>, <Right> | Arrow keys |
<C-x> | Control + x |
<S-x> | Shift + x |
<A-x> | Alt + x (or Meta on Mac) |
<M-x> | Meta + x (same as Alt) |
Notes
- Keys are processed synchronously in the order they are sent
- Special keys must be wrapped in angle brackets (e.g.,
<Esc>, notEsc) - The function respects the current Vim mode (normal, insert, visual)
- Multi-key sequences must be sent one key at a time
- For regular characters in insert mode, you can send them directly without
handleKey - The
originparameter affects how mappings are processed
See Also
- Vim.exitInsertMode() - Specifically exit insert mode
- Vim.map() - Create key mappings
- getCM() - Get CodeMirror instance from EditorView

