A more natural solution (as mentioned above) can be the to use the executeEdits method that comes with monaco and use a specific range with line numbers and column numbers, because in the examples they are always using the first line:
const startLineNumber = 15 // Line number in which the content will start being typed
const startColumn = 5 // Column number in which the content will start being typed
const endLineNumber = 18 // Line number in which the text will finish
const endColumn = 25
const text = "This is the new text" // The content going to be added
const range = new monaco.edito.Range(startLineNumber, startColumn, endLineNumber, endColumn); // It will go to the specific position of the editor
const id = { major: 1, minor: 1 }; // Required generic id
const editOperation = {identifier: id, range: range, text: text, forceMoveMarkers: true}; // The operation going to be performed
monaco.editor.executeEdits("custom-code", [ editOperation ]); // Runs the instruction
The good part of the code above is that it can add, delete, update and replace existing code, so it will be very efficient and the end user will see it like if he/she was typing.