How to insert text at caret position [Solved]

#1

How do I insert the resulting output from a script at the current caret position?

I figured out how to append by using:

var currentText = editor.getText();
editor.setText(currentText + newText);

but I can’t figure out how to simply insert where the cursor is.

0 Likes

#2

Try editor.setSelectedText("foo");

1 Like

#3

@sylumer, Thanks that works.

@agiletortoise, would be good to mention this in the documentation. It’s not really intuitive since nothing is selected, and selection usually requires setting a range.

0 Likes

#4

I don’t know that nothing is selected exactly. It is just that you have a selection that extends 0 characters - i.e. the start and the end are the same place.

0 Likes