Skip to main content
The Events API allows plugins to subscribe to editor events and execute code in response to user actions, buffer changes, and other editor state changes.

Event Subscription

on

Subscribe to an editor event.
string
required
Event to subscribe to (e.g., “buffer_save”, “cursor_moved”, “buffer_modified”)
string
required
Name of globalThis function to call with event data
boolean
true if subscription was successful
Handler must be a global function name (not a closure). Multiple handlers can be registered for the same event.
Example:

off

Unregister an event handler.
string
required
Name of the event
string
required
Name of the handler to remove
boolean
true if handler was removed successfully
Example:

getHandlers

Get list of registered handlers for an event.
string
required
Name of the event
string[]
Array of registered handler function names
Example:

Available Events

The editor emits various events that plugins can subscribe to:

buffer_save

Fired when a buffer is saved to disk

buffer_modified

Fired when buffer content is modified

cursor_moved

Fired when the cursor position changes

buffer_opened

Fired when a new buffer is opened

buffer_closed

Fired when a buffer is closed

lines_changed

Fired when lines are added, removed, or modified (batched)

Event Data

Each event passes data to the handler function. The structure depends on the event type:

buffer_save

Example:

buffer_modified

Example:

cursor_moved

Example:

lines_changed

Fired when lines are modified in a buffer. This is a batched event that’s more efficient than listening to every keystroke.
Example:

Best Practices

Prefer lines_changed over buffer_modified or cursor_moved for performance-sensitive operations like syntax highlighting.
Unsubscribe from events when your plugin is deactivated or no longer needs them to avoid memory leaks.
Wrap event handlers in try/catch blocks to prevent one plugin from breaking others.
For events like cursor_moved, consider debouncing to avoid excessive processing.

Examples

Auto-save on buffer modified

Track cursor position

Highlight TODOs on line change