Search code examples
monaco-editor

In the Monaco editor, how do I get the selected text from the IEditor interface


Getting lost in the API with it's very telegraphic documentation. How do I get the selected text from the IEditor interface? The selection object returned by getSelectiononly has line and column numbers. Do i then need to search in the editor's text for the text?

And, what is the primary selection as opposed to all other selections returned by getSelections?


Solution

  • Quite a few monaco-editor APIs work with ranges or spans. Using a range you can get the text in that range from editor.getModel().getValueInRange().

    About multiple selections: monaco-editor supports multiple selections. You first select some text without a modifier key, which becomes the primary selection. Then you add secondary selections by holding alt (opt on macOS) while dragging a new range.