Set Value for ace editor without selecting the whole editor

I'm not sure if editor.setValue() is a remnant from the old days or what, but the proper way to set an editor's content is

editor.session.setValue(text);

or

editor.getSession().setValue(text);

This will NOT select the text, so there's no need to do any of the things mentioned on this page.

editor.setValue() explicitly selects all (and forgets to unselect it); but there's no reason to use it.


You can even use clearSelection() after you do an setValue();

editor.setValue("Hello World");
editor.clearSelection(); // This will remove the highlight over the text

You can use the second parameter to control cursor position after setValue

editor.setValue(str, -1) // moves cursor to the start
editor.setValue(str, 1) // moves cursor to the end

This works for me!

editor.setValue(editor.getValue(), 1);

Tags:

Ace Editor