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 successfuloff
Unregister an event handler.string
required
Name of the event
string
required
Name of the handler to remove
boolean
true if handler was removed successfullygetHandlers
Get list of registered handlers for an event.string
required
Name of the event
string[]
Array of registered handler function names
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
buffer_modified
cursor_moved
lines_changed
Fired when lines are modified in a buffer. This is a batched event that’s more efficient than listening to every keystroke.Best Practices
Use batched events when possible
Use batched events when possible
Prefer
lines_changed over buffer_modified or cursor_moved for performance-sensitive operations like syntax highlighting.Clean up handlers
Clean up handlers
Unsubscribe from events when your plugin is deactivated or no longer needs them to avoid memory leaks.
Handle errors gracefully
Handle errors gracefully
Wrap event handlers in try/catch blocks to prevent one plugin from breaking others.
Debounce high-frequency events
Debounce high-frequency events
For events like
cursor_moved, consider debouncing to avoid excessive processing.