programking
programking

Reputation: 1386

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

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?

Upvotes: 8

Views: 10367

Answers (2)

a user
a user

Reputation: 24159

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")

Upvotes: 16

programking
programking

Reputation: 1386

Straight from API:

The method

You want to use the method navigateLineEnd(). So in your case:

editor.focus(); // To focus the ace editor
var n = editor.getSession().getValue().split("\n").length - 2;
editor.gotoLine(n); 
editor.navigateLineEnd() // Navigate to end of line

Upvotes: 8

Related Questions