Get selected text in Obsidian

Getting the currently selected text when creating an Obsidian plugin…

import { MarkdownView } from 'obsidian';

// ...

// Access to the editor component...
const editor = this.app.workspace.getActiveViewOfType(MarkdownView)?.editor; 
const selectedText = editor.getSelection(); // Get selected text

You can also update selected text…

editor.replaceSelection("New Text Goes Here");
Author: Binny V A
A philosopher programmer who specializes in backend development and stoicism.

Leave a Reply

Your email address will not be published. Required fields are marked *