100

So you can set value of an ace editor with setValue but after setting the value, the editor will select the whole value of the editor. How do you disable this? This mean when I set value of ace editor to Hello world, it won't highlight Hello world

Pixeladed
  • 1,773
  • 5
  • 14
  • 26

6 Answers6

178

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
a user
  • 23,300
  • 6
  • 58
  • 90
17

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

editor.setValue("Hello World");
editor.clearSelection(); // This will remove the highlight over the text
Harsha pps
  • 2,012
  • 2
  • 25
  • 35
  • 1
    This is not the proper way to set the editor content anymore. See Danial's answer: https://stackoverflow.com/a/65754932/8910547 – Inigo Feb 21 '21 at 23:58
  • @Inigo How come it is not a proper way? [Ace's own documentation](https://ace.c9.io/#nav=howto) still has `editor.setValue()` as the first option for setting editor content. – Ricardo Yubal Jan 02 '22 at 04:55
13

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.

Danial
  • 1,496
  • 14
  • 15
  • Well, [Ace's own documentation](https://ace.c9.io/#nav=howto) still says to use `editor.setValue()` to set content. And it also says to use `editor.getSession().setValue()` to set a value and reset undo history, but that's about it. – Ricardo Yubal Jan 02 '22 at 04:53
  • Docs are best laid plans. The truth is that editor.setValue() is a function that calls session.setValue() and does a bunch of other stuff. The session function is the raw function – Danial Jan 10 '22 at 02:54
9

This works for me!

editor.setValue(editor.getValue(), 1);
circuitry
  • 1,169
  • 16
  • 16
0
 var prevtext = $("#editor").val();
 prevtext = prevtext + "<br/>";
 $("#editor").val(prevtext).blur();
Ryan Taylor
  • 12,559
  • 2
  • 39
  • 34
Harikesh Yadav
  • 185
  • 1
  • 5
0

I've been having your same issue.

Even though you can set the second parameter to either 1 or -1, I think you should also check this: https://ace.c9.io/api/editor.html#Editor.setValue

Editor.setWrapBehavioursEnabled(Boolean enabled)

Use this right after creating the editor.

This works very well for me. The difference between this method and the one shared by a user is that the caret's position is not changed, you can move it yourself using Editor.selection.moveTo(row, column), this way the user won't experience weird caret position changes when using, say, CTRL+Z to undo an action :)

Community
  • 1
  • 1
Razvan Tanase
  • 99
  • 1
  • 7
  • 1
    This answer is not clear, do you mean that calling `Editor.setWrapBehavioursEnabled(Boolean enabled)` with some value just after the editor is initialized will prevent all text from being selected when calling `editor.setValue`? – Macario Jun 02 '17 at 23:46