Sublime Forum

Adding line numbers to a list of lines

#22

yes, that was a great start. Thanks for posting this! The offset didn’t work properly once the count got higher than 10.
This works for me…

import sublime
import sublime_plugin    

class LineNumbersAddCommand(sublime_plugin.TextCommand):
	def run(self, edit, suffix=""):
		selections = self.view.sel()
		totalOffset = 0
		for selection in selections:
			lines = self.view.split_by_newlines(selection)
			for i, line in enumerate(lines):
				line_number_string = str(i+1)+suffix
				offset = len(line_number_string)
				# print offset, "just"
				self.view.insert(edit, line.begin() + totalOffset, line_number_string)
				totalOffset += offset
0 Likes

#23

Yes there is.
Use the Incrementor package, available via Package Manager.

0 Likes