Inserting a new text at given cursor position

Chetan picture Chetan · May 19, 2014 · Viewed 16k times · Source

I am working on customizing the codemirror for my new language mode. As part of this new mode implementation, I am writing a new tool bar where user can select some text and say insert. This command should insert the text where user was typing just before clicking on tool bar.

I could not find any API level support to do so. If there is any other way can someone help me out on this?

Basically get the current cursor positio- line number and position at which cursor is currently present. May be a Position object

API for inserting a text, something like insertText("Text", PositionObject)

Answer

Sue Maurizio picture Sue Maurizio · Oct 10, 2016

Here's how I did it:

function insertTextAtCursor(editor, text) {
    var doc = editor.getDoc();
    var cursor = doc.getCursor();
    doc.replaceRange(text, cursor);
}