Home Download Buy Blog Forum Support

Duplicate Anything

Duplicate Anything

Postby ilya on Wed Jul 06, 2011 5:41 pm

Native duplicate line works only with one line, but would love to have a universal and intelligent command to duplicate not only the current line, but any selection, any selected part of code, any multiline selection. Duplicate Anything :)

For example, in JetBrain WebStorm (PhpStorm, IDEA) command Duplicate Block works this way.
ilya
 
Posts: 122
Joined: Fri Sep 11, 2009 8:32 pm

Re: Duplicate Anything

Postby dom111 on Thu Jul 07, 2011 5:46 am

Hi Ilya,

I've created a plugin that works almost exactly the same way as the TextMate Duplicate Line command works for ST2, I think it might be what you're after:

Code: Select all
import sublime, sublime_plugin

class DuplicateLineCommand(sublime_plugin.TextCommand):
  def run(self, edit):
    sel = self.view.sel()

    for region in sel:
      if region.empty():
        line = self.view.full_line(region)
        s = self.view.substr(line)
        self.view.insert(edit, line.end(), s)
        x = region.end() + len(s)
        y = x

      else:
        s = self.view.substr(region)
        self.view.insert(edit, region.end(), s)
        x = region.end()
        y = x + len(s)

    sel.clear()
    sel.add(sublime.Region(x, y))

Hope it helps!

Thanks,

Dom
dom111
 
Posts: 12
Joined: Wed Jun 08, 2011 9:13 am
Location: UK

Re: Duplicate Anything

Postby ilya on Thu Jul 07, 2011 6:08 am

Dom, thank you very much, this is exactly what I needed. ;)
ilya
 
Posts: 122
Joined: Fri Sep 11, 2009 8:32 pm

Re: Duplicate Anything

Postby atwright on Tue Jul 12, 2011 9:02 pm

Hi,

This plugin looks awesome -- but I am a lowly newbie and I haven't a clue how to install this (or create a key binding).

I tried saving it as DuplicateLineCommand.py in C:\Users\Andy\AppData\Roaming\Sublime Text 2\Packages\User

and I edited C:\Users\Andy\AppData\Roaming\Sublime Text 2\Packages\User\Default (Windows).sublime-keymap to contain:
Code: Select all
[
    { "keys": ["ctrl+shift+alt+d"], "command": "DuplicateLine" }
]


How is this meant to be done? Am I being stupid?

Many thanks,


Andy
atwright
 
Posts: 7
Joined: Tue Jul 12, 2011 8:45 pm

Re: Duplicate Anything

Postby ilya on Wed Jul 13, 2011 5:11 am

atwright, you can download last dev build, which now included this functionality out of the box: http://www.sublimetext.com/dev
ilya
 
Posts: 122
Joined: Fri Sep 11, 2009 8:32 pm

Re: Duplicate Anything

Postby dom111 on Wed Jul 13, 2011 5:33 am

Hey Andy,

Ilya is right, the same day I posted my code, a dev build was released that included (a probably much cleaner!) updated command that does the same. But for future reference, you look like you were almost right with the key binding the only change is the command name should be underscored instead of CamelCased so if you had duplicate_line it should have worked.

Hope that helps,

Cheers,

Dom
dom111
 
Posts: 12
Joined: Wed Jun 08, 2011 9:13 am
Location: UK

Re: Duplicate Anything

Postby atwright on Sat Jul 16, 2011 1:10 pm

Hi,

Thanks guys.

How do I access this new, in-built function?
atwright
 
Posts: 7
Joined: Tue Jul 12, 2011 8:45 pm

Re: Duplicate Anything

Postby dom111 on Tue Jul 19, 2011 7:55 pm

Hi Andy,

I'm not currently using dev builds on my machines, but there should be a reference to duplicate somewhere in your default or user keybindings. If you have a reference to this old plugin or something like it, you may need to remove that to get the new built-in function working. (Please, someone correct me if I'm wrong!)

Hope that helps!

Cheers,

Dom
dom111
 
Posts: 12
Joined: Wed Jun 08, 2011 9:13 am
Location: UK


Return to Ideas and Feature Requests

Who is online

Users browsing this forum: No registered users and 13 guests