Action for Duplicating Lines

Does anyone have an action for duplicating lines? Just like a text editor… hit a keyboard shortcut, and BOOM… line duplicated. :slight_smile:

Pretty straightforward. Just a few lines of code:

// get range and text of current line
let [origSt, origLen] = editor.getSelectedRange();
let [st, len] = editor.getSelectedLineRange();
let lnText = editor.getTextInRange(st, len);

// insert new line
editor.setTextInRange(st+len, 0, lnText);

// restore selection
editor.setSelectedRange(origSt, origLen);

Here’s it wrapped up in a duplicate line action.

1 Like

You are incredible!! :smiley:

Is there any chance you would be able to update the script to duplicate lines regardless of where the line is? If the last line is duplicated, it does not create a new line.

This Result
Sublime Duplicate Line

Instead of This Result
Drafts Duplicate Line