Overview
TheVim.defineAction() function creates custom Vim actions. Unlike operators (which wait for a motion), actions execute immediately when their key sequence is pressed. Actions are used for commands like entering insert mode, scrolling, or other immediate effects.
Signature
Parameters
The name of the action. This name is used to reference the action in keymaps.
The function to execute when the action is invoked. Receives:
cm(CodeMirror) - The editor instanceactionArgs(ActionArgs) - Action arguments including:repeat(number) - Repeat count from the commandforward(boolean) - Direction flagposition(string) - Position specificationafter(boolean) - Whether to act after cursor- Other action-specific arguments
vim(VimState) - The Vim state object containing:insertMode(boolean) - Whether in insert modevisualMode(boolean) - Whether in visual modevisualLine(boolean) - Whether in visual line modevisualBlock(boolean) - Whether in visual block mode- Other state properties
Return Value
This function does not return a value.
Examples
Center Screen Action
Define an action to center the screen on the current line:Toggle Comment Action
Define an action to toggle comments on the current line:Save File Action
Define an action to save the current file:Duplicate Line Action
Define an action to duplicate the current line:Jump to Definition Action
Define an action to jump to a symbol definition:Usage
Actions execute immediately when their key sequence is pressed:- Normal mode: Press the mapped keys (e.g.,
zz) - With repeat count: Prefix with a count (e.g.,
3zz) - In different contexts: Map to different modes using the
contextparameter
Notes
- Actions must be added to the keymap using
Vim.mapCommand()before they can be used - Actions execute immediately, unlike operators which wait for a motion
- The
actionArgs.repeatcontains the count prefixed before the action (e.g.,5in5zz) - Actions can modify
vimstate to switch modes or update internal state - Use actions for immediate effects; use operators for text transformations with motions
ActionArgs Interface
See Also
- Vim.defineOperator() - Define custom operators
- Vim.defineMotion() - Define custom motions
- Vim.mapCommand() - Add commands to keymap

