Text Editor API
The Text Editor API provides functionality to interact with text editors, manipulate text documents, apply edits, and control editor appearance and behavior.Core Interfaces
TextEditor
Represents an editor attached to a document. Access the active editor viawindow.activeTextEditor.
TextDocument
The document associated with this editor
Selection
The primary selection (always at index 0)
readonly Selection[]
All selections in this editor
readonly Range[]
Currently visible ranges (accounts for vertical scrolling only)
TextDocument
Represents a text document such as a source file.Working with Editors
Accessing the Active Editor
Listening to Editor Changes
Making Text Edits
Using TextEditorEdit
Theedit method provides an edit builder for making changes:
Changes in the edit builder are applied atomically when the callback completes. The builder is only valid during callback execution.
Edit Options
Control undo/redo behavior:Multiple Cursor Editing
Working with Positions and Ranges
Position Class
number
required
Zero-based line number
number
required
Zero-based character offset (UTF-16 code units)
Range Class
Selection Class
Working with Documents
Reading Document Content
Document Events
- Document Changes
- Document Open/Close
- Document Save
Text Decorations
Creating Decoration Types
Applying Decorations
Inserting Snippets
SnippetString
Variable Snippets
Revealing Ranges
Editor Options
number | string
Size of a tab in spaces (or “auto”)
boolean | string
Insert spaces when pressing Tab (or “auto”)
TextEditorCursorStyle
Rendering style of the cursor
TextEditorLineNumbersStyle
Line number display style
Common Patterns
Transform Selected Text
Insert Text at Cursor
Replace Text in Document
Best Practices
Performance
Performance
- Batch multiple edits in a single
edit()call - Use
getText()with ranges instead of getting all text - Dispose decoration types when no longer needed
- Cache document references when processing multiple times
User Experience
User Experience
- Respect user’s tab/space settings
- Preserve selections when possible
- Use appropriate undo stops for complex operations
- Provide feedback for long-running operations
Correctness
Correctness
- Always validate positions and ranges
- Handle case when no active editor exists
- Account for multi-cursor scenarios
- Use UTF-16 code units for character offsets
Related APIs
- Commands API - Register text editor commands
- Workspace API - Open and manage documents
- Languages API - Provide language features