Sublime Forum

Duplicate line/selection ends up in the wrong spot

#1

The Duplicate Line command works fine if I’m on a single line and don’t have anything selected. However when I try to duplicate a selection instead of placing the selected text on a new line it places it at the end of the selection. For instance if I have the following selected:

duplicate this line
and this one as well

it will produce:

duplicate this line
and this one as wellduplicate this line
and this one as well

anyway around this? it’s extremely annoying :angry:

1 Like

#2

If you select the newline at the end of the second line it’ll act as you ask. Doing an Expand Selection to Line before a Duplicate is one way to do this.

0 Likes

#3

ok cool. why wouldn’t you always do expand to line? it seems like that’s the expected behavior…shouldn’t that just be part of the command?

1 Like

#4

You can chain the two commands together with a macro and bind the macro to a keybinding if you want.

0 Likes

#5

You can do that but now if I want to run the command twice it’ll grab the next line down.

Original (I’ll select and duplicate the top two lines):

it { should respond_to(:password_digest) } it { should respond_to(:email) } it { should be_valid }

After 1st run (This is correct so far):

it { should respond_to(:password_digest) } it { should respond_to(:email) } it { should respond_to(:password_digest) } it { should respond_to(:email) } it { should be_valid }

After 2nd run (This is not correct. Because we’re expanding the selection to line every time it grabbed the next line down and duplicated it too)

it { should respond_to(:password_digest) } it { should respond_to(:email) } it { should respond_to(:password_digest) } it { should respond_to(:email) } it { should be_valid } it { should respond_to(:password_digest) } it { should respond_to(:email) } it { should be_valid }

0 Likes

#6

Sorry for the necro but this seemed the most relevant thread about this topic.

Fixed this annoying behavior with this macro:

[
    {"command": "expand_selection", "args": {"to": "line"}},
    {"command": "duplicate_line"}
]

I saved it in Packages/User/duplicate-lines.sublime-macro.

And then on the keymap file:

{ "keys": ["super+alt+down"], "command": "run_macro_file", "args": {"file": "res://Packages/User/duplicate-lines.sublime-macro"} }
0 Likes