Skip to content

Instantly share code, notes, and snippets.

@bobspryn
Created April 8, 2011 21:34
Show Gist options
  • Save bobspryn/910794 to your computer and use it in GitHub Desktop.
Save bobspryn/910794 to your computer and use it in GitHub Desktop.
Handles multiple lines and leaves your cursor on the newly created end line. Accepts a direction that you want to duplicate your lines of text
import sublime, sublime_plugin
class DuplicateLineCommand(sublime_plugin.TextCommand):
def run(self, edit, direction='down'):
self.view.run_command("expand_selection", {"to": "line"})
view = self.view
for region in self.view.sel():
line = region
if line.empty():
line = self.view.full_line(region)
line_contents = self.view.substr(line)
if(direction=='down'):
self.view.insert(edit, line.begin(), line_contents)
newline = sublime.Region(line.b, line.b+line.size()-1)
else:
self.view.insert(edit, line.end(), line_contents)
newline = sublime.Region(line.a, line.a+line.size()-1)
self.view.sel().clear()
self.view.sel().add(newline)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment