Skip to content
ZeroServer.tools

Add Line Numbers

Prefix every line with a sequential line number.

Lines: 0Characters: 0

Numbering lines of text

Line numbers make it easy to reference specific rows in logs, code snippets, lists, and documentation. This tool prefixes each line with a running number using your chosen starting value and separator, and can right-pad the numbers so every line aligns neatly regardless of digit count. Everything is processed locally in your browser.

Built and maintained by Meet Shah · Last updated

What this tool is used for

  • Numbering a code excerpt so you can refer to a specific line in review.
  • Preparing a transcript where lines need citing.
  • Numbering a list before sharing it so items can be referenced.
  • Producing a numbered listing for documentation.
  • Adding numbers to a log extract to discuss a particular entry.

Frequently Asked Questions

Should numbering start at 0 or 1?
Editors and compilers number lines from 1, and that is the convention here — a compiler error on "line 42" refers to the 42nd line. Array indices start at 0, which is a different thing entirely and a frequent source of off-by-one confusion.
Why pad the numbers?
So the code stays aligned. Without padding, the jump from line 9 to 10 shifts every subsequent line right by a character. Right-aligning to the width of the largest number keeps a clean gutter.
Does the final newline count as a line?
This is genuinely ambiguous and tools disagree. POSIX defines a line as ending WITH a newline, so a trailing newline does not begin a new line — but many editors display a phantom empty last line anyway. It explains off-by-one differences between tools.
Will the numbers break copy-paste of code?
Yes — numbered output is for reading and referencing, not for running. Anyone copying it must strip the numbers again, which is why code blocks on the web usually render numbers in a separate non-selectable column.
What separator should I use?
A colon and space matches compiler and grep output (file:42: message), making the result greppable. A pipe or tab suits side-by-side reading. Consistency matters more than the specific choice.
How should numbering handle a wrapped line?
As one line — the number belongs to the logical line, not the visual one. Editors that number visual rows make a wrapped file confusing to discuss, which is why most number logical lines and wrap without a number.
Should numbering restart per section?
Only if the sections are independently referenced. Continuous numbering is easier to cite across a whole file; per-section numbering is what legal and specification documents use, where the section is the unit.
What about numbering only non-blank lines?
It is what `cat -b` does, and it makes for a tidier listing — but it breaks the correspondence with an editor's own line numbers, which is usually the reason for numbering in the first place.

Common errors and gotchas

  • Numbering from one when the excerpt starts partway through a file, so the numbers mislead.
  • Adding numbers to code that someone will copy and run, which then fails to parse.
  • Numbering wrapped lines as separate lines, which desynchronises from the source.
  • Losing the numbers' alignment on files past a hundred lines without padding.
  • Numbering a file that already has numbers, producing two columns of them.

Related Text Tools tools

Private & free — this tool runs entirely in your browser.