Add Line Numbers to Text

Add numbering to each line of text or strip existing numbers back off, with control over the start value, padding, separator and whether blank lines count.

The padding on line 1 is decided by how many lines come after it

Zero-padded numbering needs to know how wide the widest number will be, and the widest number is the last one. So the width of the very first label is a function of the length of the whole file:

LinesWidthFirst label
9 1 1
10 2 01
99 2 01
100 3 001
999 3 001
1000 4 0001

Adding one line to a 999-line file re-pads every line in it. That is correct — it is what alignment means — and it is worth knowing before diffing two numbered outputs and finding every row changed for no reason you can see.

"Skip blank lines" does not just skip them either. It reduces the count of numbered lines, which lowers the last number, which can narrow the width. A file of 1000 alternating blank and non-blank lines numbers to width 4 with the option off and width 3 with it on — so a setting that reads as "which lines get a number" silently changes the alignment of every number that remains.

A negative start overflows the column

StartLastComputed widthLabel needs
-5 -3 1 2 — overflows
-100 -96 3 4 — overflows
-1 18 2 2

The width comes from the absolute values of the first and last numbers, so the minus sign is not counted. Start at −100 and the width comes out 3 while -100 is four characters wide. Nobody numbers lines from −100 often; it is worth a line here because the cause is a single Math.abs and it is the kind of thing that looks perfect in every test that starts at 1.

Removing numbers is eager one way and shy the other

Stripping numbers back off looks for digits, an optional separator, and then whitespace. The separator is optional and the whitespace is not, which produces two opposite failures:

InputOutput
1. first item first item
7) indented indented
12: colon style colon style
1984 was a year was a year lost a real number
42 is the answer is the answer lost a real number
3.14 is pi 3.14 is pi
1.text 1.text kept a list number
no number here no number here

2 lines of ordinary prose lose a number that was never a line number, and 1 genuine list item keeps its number because there is no space after the point. A decimal survives, because the digit after the point is not whitespace.

There is no clean fix inside a regular expression — "1984 was a year" and "1984 the year it was written" are the same shape and only one of them is a line number. Worth knowing this pass is a heuristic, and worth reading the output when the text starts with figures.

How to use

  1. Paste your text.
  2. Choose the starting number, padding and separator.
  3. Decide whether blank lines should be numbered.
  4. Switch to strip mode to remove existing numbers.

Frequently asked questions

Why pad the numbers with zeros?

So they align. Without padding, line 9 and line 10 start the actual text at different columns, which makes a numbered listing look ragged and harder to scan. Padding to the width of the largest number keeps everything lined up.

Should blank lines be numbered?

Depends on the purpose. For quoting a document so someone can refer to a line, number everything, since that matches what an editor shows. For producing a numbered list of items, skip the blanks — they are separators, not entries.

How do I remove numbers that are already there?

Use strip mode, which detects leading numbers followed by a separator and removes them. It has to guess at the format, so unusual separators may need checking. This is the usual way to recover copyable code from a numbered listing.

Why can't I copy code from a numbered listing?

Because the numbers come along with it and have to be stripped before the code will run. Some documentation sites make the numbers unselectable to avoid this; many do not, which is exactly why strip mode is useful.

Can I start from a number other than one?

Yes. This matters when numbering an excerpt from the middle of a file, where starting at one would misrepresent where the lines actually sit.

What separator should I use?

A colon and a space suits code and log output and matches most editors. A full stop suits prose lists. A tab is best when the result is going into a spreadsheet, since it puts the numbers in their own column.

🔒 This tool runs entirely in your browser. Nothing you enter is uploaded, logged, or stored.