Overview
TheEditor class provides a common interface for interacting with the Obsidian editor, abstracting away the differences between CodeMirror 5 and CodeMirror 6. This interface allows plugins to manipulate text, selections, and editor state in a version-independent way.
Available since version 0.11.11
Properties
The Editor class exposes all methods through its instance and doesn’t have public properties that need to be accessed directly.Methods
getDoc()
Returns the document instance (the Editor itself).Returns this editor instance
refresh()
Refreshes the editor display.getValue()
Gets the entire content of the editor.The complete text content of the editor
setValue()
Sets the entire content of the editor.The new content to set in the editor
getLine()
Gets the text at a specific line (0-indexed).The line number (0-indexed)
The text content of the specified line
setLine()
Sets the text at a specific line.The line number (0-indexed)
The new text for the line
lineCount()
Gets the total number of lines in the document.The number of lines in the document
lastLine()
Gets the index of the last line in the document.The index of the last line (0-indexed)
getSelection()
Gets the currently selected text.The selected text
somethingSelected()
Checks if there is any text currently selected.True if text is selected, false otherwise
getRange()
Gets the text between two positions.The starting position
The ending position
The text between the two positions
replaceSelection()
Replaces the currently selected text.The text to replace the selection with
Optional origin identifier for the change
replaceRange()
Replaces text in a specific range.The replacement text
The starting position
The ending position (optional)
Optional origin identifier for the change
getCursor()
Gets the current cursor position.Which side of the selection to get (optional)
The cursor position
listSelections()
Gets all current selections.Array of all current selections
setCursor()
Sets the cursor position.The position to set the cursor to, or a line number
The column number (if pos is a line number)
setSelection()
Sets a selection between two positions.The anchor position (start of selection)
The head position (end of selection, optional)
setSelections()
Sets multiple selections at once.Array of selection ranges
Index of the main selection (optional)
Available since version 0.12.11
focus()
Gives focus to the editor.blur()
Removes focus from the editor.hasFocus()
Checks if the editor currently has focus.True if the editor has focus
getScrollInfo()
Gets the current scroll position.Object containing scroll position
scrollTo()
Scrolls the editor to a specific position.Horizontal scroll position (null to keep current)
Vertical scroll position (null to keep current)
scrollIntoView()
Scrolls the editor to make a specific range visible.The range to scroll into view
Whether to center the range in the viewport (optional)
Available since version 0.13.0
undo()
Undoes the last change.redo()
Redoes the last undone change.exec()
Executes a built-in editor command.The command to execute
Available since version 0.12.2
goUp,goDown,goLeft,goRightgoStart,goEndgoWordLeft,goWordRightindentMore,indentLessnewlineAndIndentswapLineUp,swapLineDowndeleteLinetoggleFold,foldAll,unfoldAll
transaction()
Executes a transaction containing multiple changes.The transaction to execute
Optional origin identifier
Available since version 0.13.0
wordAt()
Gets the word at a specific position.The position to check
The range of the word, or null if no word found
posToOffset()
Converts an EditorPosition to a character offset.The position to convert
The character offset
offsetToPos()
Converts a character offset to an EditorPosition.The character offset to convert
The corresponding position
processLines()
Processes lines in the document, reading and optionally modifying them.Function to read/analyze each line
Function to optionally modify lines based on read results
Whether to skip empty lines (optional)
Available since version 0.13.26