Sublime Forum

Adding line numbers to a list of lines

#3

Thanks for the coding suggestion – I am looking at the ST API Reference page to get a handle on it.

0 Likes

#5

Also have a look on Text Pastry

0 Likes

#6

Textpad provides by far the easiest way to add line numbers at the start of lines – simply find “^” and replace with "\i. " (a regular expression without the quotes). Then create a macro to apply this expression on selected lines. Simple and compact – one click to run.

0 Likes

#7

I like text pastry better, although for just adding the line numbers, there is an extra step of course. Time wise, not much difference?

Select the area, split into lines, command-P and add numbers x to y

0 Likes

#8

My philosophy of user interfaces is that every step that can be eliminated absolutely must be eliminated. It’s all about the Darwinian survival of the fittest in the competition among tools for ease of use. Every reduction of friction provides an advantage.

The Textpad approach is slicker and more minimal, in my opinion.

But Text Pastry is a great package for numerous operations.

0 Likes

#9

I personally use Minipy, which evaluates python and can also used for this issue.
Just select the text, split the selection (ctrl+shift+l), insert and select $ at the start of the line, and evaluate python (ctrl+shift+x) to get increasing numbers.
This can also recorded as a macro to have it in one click.

2 Likes

#10

When I recorded this as a macro, Minipy made the assumption that all future selections would include the same number of lines as the current selection.

0 Likes

#11

Start record the macro after selecting the text and before pressing ctrl+shift+l works for me:

3 Likes

#12

You’re right – that sequence works.

What are some other cool Minipy tip and tricks you would recommend?

0 Likes

#13

https://packagecontrol.io/packages/Insert%20Nums

2 Likes

#14

 
Have you tried Copy With Line Numbers Reloaded?

You pretty much get the result you’re looking for with one step, and then you can paste the output straight into its final destination.

It also works with multiple selections and has a few useful options @ sublime-settings.

1 Like

#15

@seanmcbride

BTW, what is your intended output for this?

ExportHTML is awesome for extracting your code + line numbers + color scheme to html.

1 Like

#16

The package just evaluates python code using eval. You can do a lot of stuff, if you know python (there are similar packages for other languages as well). I mostly use it

  • for stuff with increasing numbers as in your example
  • for inline calculations without using an external tool, e.g. calculate 992 + 345 or binary/hex strings bin(123) or …
  • for creating repeating characters, e.g. create 37 a’s by writing "a"*37

However you can even open the current file in firefox: exec('import subprocess; subprocess.Popen(["firefox", view.file_name()])')

2 Likes

#17

By the way, how did you capture that graphic above? Nice.

0 Likes

#18

I used ScreenToGif for that.

2 Likes

#19

@seanmcbride   @r-stein

Check out ActivePresenter.  I used ScreenToGIF as my primary for a while, but now use AP since it has a much easier to use timeline ( perfect for removing unnecessary frames & sequencing annotations ) & an awesome set of annotation features ( customizable shape presets, fade in/out, shadows, etc. ).  It doesn’t export directly to GIF, so I still run the exported video through ScreenToGIF.

0 Likes

#20

Thanks for the pointer.

0 Likes

#21

How do I install this copy with line numbers reloaded?

0 Likes

#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