How to move the cursor to the end of the line in ACE Editor?

Luke Shimkus picture Luke Shimkus · Dec 23, 2014 · Viewed 8.5k times · Source

Inside my Javascript file I have the following lines of code:

editor.focus(); // To focus the ace editor
var n = editor.getSession().getValue().split("\n").length - 2;
editor.gotoLine(n); 

This focuses the ACE editor, moves to the last line, and goes up two lines. When it goes up those two lines it also moves the cursor to the front of that line. Simulating someone pressing the Home key.

Q: How do I move the cursor to the end of the line? Or simulate the End Key?

Answer

a user picture a user · Dec 23, 2014

gotoline takes two arguments one for row and one for column

var row = editor.session.getLength() - 1
var column = editor.session.getLine(row).length // or simply Infinity
editor.gotoLine(row + 1, column)

Note that gotoLine scrolls selection into view with an animation, If you do not want that you can use

editor.selection.moveTo(row, column)

instead.

To simulate the end key exactly the way it is handled when the user presses end use

editor.execCommand("gotolineend")