Skip to content

Instantly share code, notes, and snippets.

@Kalli
Created July 22, 2012 23:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kalli/3161353 to your computer and use it in GitHub Desktop.
Save Kalli/3161353 to your computer and use it in GitHub Desktop.
Insert Line Numbers Sublime Text 2 Plugin
'''Sublime Text 2 plugin for inserting the line numbers of a text document or a selection within it '''
import sublime
import sublime_plugin
class InsertLineNumbersCommand(sublime_plugin.TextCommand):
def run(self, edit):
selected_region = self.view.sel()[0]
if selected_region.size() == 0:
text = self.view.substr(sublime.Region(0, self.view.size()))
else:
text = self.view.substr(sublime.Region(selected_region.begin(), selected_region.end()))
lines = text.split("\n")
text = ""
for idx, val in enumerate(lines):
text = text + str(idx + 1) + ". " + val + "\n"
if selected_region.size() == 0:
self.view.replace(edit, sublime.Region(0, self.view.size()), text)
else:
self.view.replace(edit, sublime.Region(selected_region.begin(), selected_region.end()), text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment