Sublime Forum

Duplicate Anything

#1

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 :smile:

For example, in JetBrain WebStorm (PhpStorm, IDEA) command Duplicate Block works this way.

0 Likes

#2

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:

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

0 Likes

#3

Dom, thank you very much, this is exactly what I needed. :wink:

0 Likes

#4

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:

{ "keys": "ctrl+shift+alt+d"], "command": "DuplicateLine" } ]

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

Many thanks,

Andy

0 Likes

#5

atwright, you can download last dev build, which now included this functionality out of the box: sublimetext.com/dev

0 Likes

#6

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

0 Likes

#7

Hi,

Thanks guys.

How do I access this new, in-built function?

0 Likes

#8

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

0 Likes