How to add new line programmatically in CodeMirror?
codemirror
Solution
Get the current line from the cursor position, and act on it. This should do it (not tested):
var doc = cm.getDoc();
var cursor = doc.getCursor(); // gets the line number in the cursor position
var line = doc.getLine(cursor.line); // get the line contents
var pos = { // create a new object to avoid mutation of the original selection
line: cursor.line,
ch: line.length - 1 // set the character position to the end of the line
}
doc.replaceRange('my new line of code\n', pos); // adds a new line
Problem
I need to insert a new line next to my current line number in CodeMirror. I viewed the documentation but didn't find anything about appending any content at the end of the line. Please help. :(