Search code examples
javascriptace-editor

Ace Editor API: How to select line 2?


I want to select line 2 for copy & paste in ACE. There is a method selectLine(), which is documented here: http://ace.c9.io/#nav=api&api=selection but i don't understand how to use it. Unfortunately its also nothing to find on stackoverflow.com about selection, only about highlighting, which is not the same.

// ACE Editor Setup
var editor = ace.edit("editor");
editor.setTheme("ace/theme/crimson_editor");
editor.getSession().setMode("ace/mode/html");
editor.setValue("textline1\n textline2\n textline3");

var select = new Selection(editor.getSession());    // Uncaught TypeError: Illegal constructor
select.selectLine(2);

Solution

  • Once ace is initialized, it creates a Selection object instance therefore you don't need to recreate it. To access Selection just use editor.selection.

    Another important point is selectLine selects the current line (it doesn't accept any parameters). So to move the cursor and select the line you have to first use moveCursorToPosition function.

    Here is an example:

    editor.selection.moveCursorToPosition({row: 1, column: 0});
    editor.selection.selectLine();