Line numbers turn unstructured text into something you can reference. Instead of saying "the part where it mentions the deadline," you say "line 14." That precision matters in code reviews, legal citations, academic analysis, and collaborative editing.
This guide covers when and why you need line numbers, the formatting conventions for different contexts, and common mistakes to avoid.
Why Line Numbers Matter
Line numbers solve a coordination problem. When two people look at the same document, they need a shared way to point at specific locations. Page numbers work for printed material, but within a single page or a code file, line numbers are the standard.
Where line numbers are essential
- Code — every IDE and text editor shows line numbers. Error messages reference them. Code reviews discuss "line 47."
- Legal filings — many courts require numbered lines on pleadings and transcripts. Opposing counsel references arguments by line.
- Poetry and lyrics — literary analysis cites lines by number. "In line 3, the speaker shifts tone…"
- Log files — when debugging, "the error at line 12,047" is actionable. "Somewhere in the logs" is not.
- Screenplays and scripts — line numbers help actors and directors coordinate during rehearsal.
- Academic texts — peer reviewers reference specific lines in manuscripts.
Line Numbering Conventions by Context
Different fields use different formatting. Choosing the wrong style makes your output look amateur.
Code and programming
Most programming tools use these conventions:
- Start at line 1 (some languages use 0-indexed arrays, but line numbers in editors start at 1)
- Right-aligned numbers with space padding for consistent indentation
- Tab or space separator between the number and the code
- Number every line, including blank lines — blank lines are part of the structure
Example:
1 function greet(name) {
2 if (!name) {
3 return 'Hello, stranger';
4 }
5 return `Hello, ${name}`;
6 }
Legal documents
Court filings in many jurisdictions require:
- Lines numbered 1 through 28 (a common US court standard per page)
- Numbers in the left margin, right-aligned
- Colon or period separator
- Every line numbered, including blank lines between paragraphs
Poetry and lyrics
Literary conventions typically use:
- Numbers every 5th line (5, 10, 15, 20…) in published editions
- All lines numbered in analysis or classroom settings
- Period separator (e.g., "1. Shall I compare thee to a summer's day?")
- Empty lines between stanzas are not numbered
Log files and data
For debugging and data analysis:
- Zero-padded numbers keep columns aligned in large files (001, 002, …, 999)
- Pipe separator (
|) works well for visual scanning - Number every line — empty lines in logs often signal events
Formatting Options Explained
Starting number
Most line numbering starts at 1. Exceptions:
- Starting at 0 is common when referencing array indices or memory offsets
- Starting at a custom number helps when you're numbering a continuation (e.g., lines 51–100 of a longer document)
Separators
The separator sits between the number and the text. Common choices:
| Separator | Example | Best for |
|---|---|---|
Colon + space (: ) |
1: Hello world |
General purpose, readable |
Period + space (. ) |
1. Hello world |
Prose, legal text |
Tab (\t) |
1→Hello world |
Code, monospaced output |
Pipe (|) |
1 | Hello world |
Log files, data analysis |
Parenthesis () ) |
1) Hello world |
Informal lists |
Pick one and stay consistent within a document.
Padding and alignment
When line numbers have different digit counts (1 vs. 100 vs. 1000), unpadded numbers create ragged output:
1: First line
2: Second line
...
99: Ninety-ninth line
100: Hundredth line
The text shifts right when the number width changes. Three padding approaches fix this:
Right-aligned with spaces (most common for code):
1: First line
2: Second line
99: Ninety-ninth line
100: Hundredth line
Zero-padded (good for fixed-width output and sorting):
001: First line
002: Second line
099: Ninety-ninth line
100: Hundredth line
No padding (acceptable only for short texts under ~9 lines):
1: First line
2: Second line
Skipping empty lines
Two schools of thought:
- Number all lines (including empty ones) — preserves exact line counts. If someone says "line 15," it's always the same physical position. This is the code convention.
- Skip empty lines — only content lines get numbers. Useful for prose or poetry where blank lines are formatting, not content. The downside: line numbers don't correspond to physical positions.
Common Mistakes
Inconsistent separators
Mixing : and . in the same document is confusing. Pick one format before you start.
Forgetting padding on long files
A 500-line file without padding produces misaligned output starting at line 10, then again at line 100. Always use padding for files over 9 lines.
Numbering partial excerpts without context
If you paste lines 200–250 of a file but number them 1–50, you lose the original line references. Set the starting number to 200 to preserve context.
Using the wrong starting index
Some contexts expect 0-based numbering (data arrays), others expect 1-based (human-readable documents). Using the wrong one creates off-by-one confusion.
How to Add Line Numbers: Methods
In a text editor
Most code editors show line numbers by default. To enable them:
- VS Code:
Editor: Line Numberssetting (on by default) - Sublime Text:
View → Line Numbers - Vim:
:set numberor:set relativenumber - Notepad++:
View → Show Symbol → Show Line Number Margin
These display numbers but don't embed them in the text.
On the command line
To add line numbers to file output:
cat -n file.txt— numbers all lines, right-aligned, tab separatornl file.txt— numbers non-empty lines by default;nl -ba file.txtnumbers all linesawk '{print NR": "$0}' file.txt— customizable formatgrep -n "" file.txt— numbers all lines with colon separator
With a line numbering tool
Online line numbering tools work when you need to:
- Number text quickly without installing anything
- Customize the format (separator, padding, starting number)
- Process text you received in an email or document (not already in a code file)
The Line Numberer on this site runs entirely in your browser — no data is uploaded to any server.
Worked Examples
Example 1: Code snippet for documentation
Input:
const add = (a, b) => a + b;
const subtract = (a, b) => a - b;
module.exports = { add, subtract };
Settings: Start at 1, tab separator, space padding, number all lines.
Output:
1 const add = (a, b) => a + b;
2 const subtract = (a, b) => a - b;
3
4 module.exports = { add, subtract };
Example 2: Poetry for classroom analysis
Input:
Two roads diverged in a yellow wood,
And sorry I could not travel both
And be one traveler, long I stood
And looked down one as far as I could
To where it bent in the undergrowth;
Settings: Start at 1, period separator, space padding, right-align.
Output:
1. Two roads diverged in a yellow wood,
2. And sorry I could not travel both
3. And be one traveler, long I stood
4. And looked down one as far as I could
5. To where it bent in the undergrowth;
Example 3: Server log excerpt starting at line 847
Input:
[INFO] Connection established
[WARN] Timeout approaching: 28s
[ERROR] Connection reset by peer
[INFO] Reconnecting...
Settings: Start at 847, pipe separator, zero padding, number all lines.
Output:
0847 | [INFO] Connection established
0848 | [WARN] Timeout approaching: 28s
0849 | [ERROR] Connection reset by peer
0850 | [INFO] Reconnecting...
FAQ
What is a line numberer?
A line numberer is a tool that prepends sequential numbers to each line of text. It takes plain text as input and outputs the same text with a number before each line, using a configurable separator and formatting style.
How do I add line numbers to a text file?
Paste the text into a line numbering tool or use a command-line utility like cat -n or nl. Choose your preferred separator (colon, period, tab), padding style (spaces, zeros, none), and starting number. The tool outputs the numbered text which you can copy.
Should I start numbering at 0 or 1?
Start at 1 for human-readable documents, code snippets, legal filings, and poetry. Start at 0 only when referencing array indices, memory offsets, or other zero-based technical contexts.
What separator should I use between the number and text?
Use a tab for code (matches IDE conventions), a colon for general purpose, a period for prose and legal text, and a pipe for log files and data. The key rule: pick one and stay consistent.
How do I keep line numbers aligned for large files?
Use padding. Right-aligned space padding works for most cases. Zero-padding (01, 02, …, 99) works well for fixed-width output and filenames that need to sort correctly.
Should I number empty lines?
Number all lines when the physical position matters (code, log files). Skip empty lines when they're just formatting (poetry stanzas, prose paragraphs).
Can I number lines starting from a specific number?
Yes. Set the starting number to match the original position. This is useful for excerpts — if you paste lines 200–250, start numbering at 200 to preserve original references.
How do legal documents use line numbering?
Many courts require pleadings with numbered lines, typically 1–28 per page. Numbers appear in the left margin, right-aligned, with consistent spacing. The exact requirements vary by jurisdiction.
Do line numbers change the text content?
Line numbering prepends characters to each line. The original text is unchanged — only the beginning of each line has the number and separator added. To remove them later, strip the prefix pattern.
Is there a standard line numbering format?
There is no universal standard. Conventions vary by field: code uses tabs with right-aligned numbers, legal uses margin numbers, poetry uses periodic numbering (every 5th line in published work). Choose the convention that matches your context.