50 votes
 
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.
 
COMPLETED
updated 2 years ago

 This was done in build 2091

 
0
Sami Pirbay
Very useful because it doesn't modify the clipboard
Translation provided by Microsoft translator:

 
+3
Oktay Acikalin
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)
Translation provided by Microsoft translator:

 
0
Andreas Timm
Very well, thank you.
Translation provided by Microsoft translator:

 
0
Oktay Acikalin
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.
Translation provided by Microsoft translator:

 
0
Eric Reiche
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
Translation provided by Microsoft translator:

 
0
Oktay Acikalin
Thanks Eric :).
Translation provided by Microsoft translator:

 
0
iconv

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

Translation provided by Microsoft translator:

 
0
aristidesfl

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

Translation provided by Microsoft translator:

 
+2
ybakos

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


Translation provided by Microsoft translator:

Community stats

  • 26,722People
  • 3,763Feedback
  • 5,616Comments
  • 63,231Votes