Skip to main content

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 via window.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

The edit 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

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

  • 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
  • Respect user’s tab/space settings
  • Preserve selections when possible
  • Use appropriate undo stops for complex operations
  • Provide feedback for long-running operations
  • 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