Add Line Numbers

Three settings decide everything here: where the numbering starts, how much it steps each line, and how wide the number is padded. Start at 11 to continue a list, step by 10 to leave room for insertions, pad to three so that 9 and 10 line up instead of stepping sideways, and the rest is a separator choice.

Add Line Numbers — Number a List with a Start, Step, Padding and SeparatorBuildFigure

Start and step

The start value does not have to be 1. Continue a list that already runs to ten by starting at 11; number from 0 if the list mirrors array positions; start negative if you are labelling something relative to a zero point. The step controls the interval: 2 gives odd or even numbers, 10 gives 10, 20, 30 and leaves nine free slots between each pair for anything you insert later, which is the same reasoning behind line numbers in early BASIC.

A negative step counts down. Start at 10 with a step of −1 and you get a countdown, or a ranked list numbered from the bottom up. A step of 0 puts the same number on every line, which is almost always a mis-typed field rather than an intention, so the page flags it.

Why padding is not cosmetic

Two things go wrong without it. Visually, the moment the count crosses from 9 to 10 the text after the number shifts one column right, and across a hundred lines the left edge of the content looks like a staircase. Functionally, anything that later sorts those lines as text will order them by character rather than by value, giving 1, 10, 11, 2, 3 — the classic wrong file-list order. Zero-padding to a fixed width removes both problems, and automatic padding works out the width from the largest number the list will actually reach.

If you expect the list to grow, choose a fixed width one digit larger than you need today. Renumbering later is cheap here but reconciling it with names or filenames already in use elsewhere is not.

Zeros, spaces and the minus sign

Zero padding and space padding place the minus sign differently, and the difference is deliberate. With zeros the minus stays at the far left and the zeros sit between it and the digits, so −1 at width three reads -001. With spaces the whole token including the minus is pushed right, so the numbers stay right-aligned in a monospaced column and the minus signs line up with the digits above them. Zero padding suits identifiers and filenames; space padding suits anything you are going to read as a column.

Space padding has one trap worth knowing: combined with the tab separator, the leading spaces travel into the spreadsheet cell, and a cell whose content starts with a space is text, not a number. Use zeros or no padding for spreadsheet output.

Blank lines, and what the numbers are

Skipping blank lines is the default and suits prose with paragraph breaks: the gaps stay as gaps and the numbering only advances on lines with content. Numbering them anyway reproduces a source file's own line numbering, which is what you want when the numbers have to correspond to a code listing or a log. Deleting them tightens a list up. A line containing only spaces or tabs counts as blank in all three modes.

Whatever you choose, the result is plain text. The numbers are characters in the line, not a list format, which is what makes them survive pasting into an email, a chat message or a plain-text field where a word processor's automatic numbering would either vanish or renumber itself. The corollary is that reordering the lines afterwards does not renumber anything, and this tool has no way to strip existing numbers back off — do the sorting first. It handles 10,000 lines and reports truncation beyond that.

Questions people ask

Can I start at 0, or use negative numbers?

Both. A start of 0 numbers the first line 0, which suits anything mirroring zero-based positions. Negative starts work too, and negative steps count downward from wherever you began. When negatives are involved the padding applies to the digits, so with zero padding at width two you get -01 rather than a shifted -1; space padding instead right-aligns the whole token including the minus.

If I reorder the lines afterwards, do the numbers follow?

No. The numbers are ordinary characters once written, so moving a line moves its old number with it. Do everything that changes line order — sorting, deduping, filtering — before numbering, and treat this as the last step. If you have already numbered and then reordered, you will need to strip the numbers and start again.

Can it remove numbers that are already there?

No, it only adds. If the existing numbering is consistent, strip it with the regex-capable multi find and replace — a pattern anchored to the start of the line that eats digits and the separator — and then number the clean text here. If the existing numbering is inconsistent, editing by hand is often faster than describing the inconsistency to a pattern.

Does it do multi-level numbering like 1.1 and 1.2?

No. Every line is treated as being at the same level and gets a single number. Nothing here reads indentation or infers a hierarchy, so an outline pasted in comes back as a flat numbered list. Multi-level numbering needs a tool that decides which line is a child of which, and that is a different job from putting a counter in front of each row.

Related