+52
Lokið

Ctrl + Shift + D should duplicate not only current line, but current selection if available

Metin Amiroff 13 ár síðan updated by Jon Skinner 13 ár síðan 9
Currently the only way to duplicate a block of code is to go through a tedious process of select->copy->go down->unindent if needed->paste.

ST1 had a plugin (poweredit) that made Ctrl+Sift+D duplicate currently selected text if it's available. If not, it would duplicate current line. This is super useful and productive feature and I think must come by default with ST2.

Thanks.

Answer

Answer
Lokið

 This was done in build 2091

+1
Very useful because it doesn't modify the clipboard
+5
I've hacked the existing duplicate_line.py a bit. It copies the current line if nothing selected. Otherwise it inserts the selection after the selection. For duplicating complete lines you have to select the whole line (including the newline character).


import sublime_plugin

class DuplicateLineCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        for region in self.view.sel():
            line = region
            if line.empty():
                line = self.view.full_line(region)
            line_contents = self.view.substr(line)
            self.view.insert(edit, line.end(), line_contents)
+1
Very well, thank you.
+2
I've seen that some of my co-workers did indent it wrongly. You have to indent the last two lines at the same column like the "if line.empty()" is, otherwise nearly nothing will happen.
As always you install it by putting it into your User folder.
+1
Very nice, thank you.
here is a screenshot for the indentation for those who don't know python too well:
http://twitpic.com/3y9lt7

The script shall be put for a default Mac OS X installation under ~/Library/Application Support/Sublime Text 2/Packages/User/duplicate_line.py
+1

+1, and there is some inconsistency while 'Delete Line' operates on selection but 'Duplicate Line' doesn't.

+1

I believe this has been implemented already.. should't it be closed? 

+1

The latest version of ST2 supports this out of the box: Cmd-Shift-D.


Answer
Lokið

 This was done in build 2091