Line Number Adder
Add line numbers to text with customizable starting number, padding, and separator.
Back to all tools on ToolForge
Input Text
Options
Result
About Line Number Adder
This line number adder prefixes each line of text with an incrementing number, supporting customizable formatting for different use cases. Options include starting number, zero-padding width, and separator style.
Line Numbering Algorithm
JavaScript Line Numbering Implementation:
function addLineNumbers(text, options) {
const {
startNum = 1,
padding = 3,
separator = '\t'
} = options;
// Split on newlines (handles \r\n and \n)
const lines = text.split(/\r?\n/);
// Add line numbers
return lines.map((line, index) => {
const num = startNum + index;
const padded = padding > 0
? String(num).padStart(padding, '0')
: String(num);
return padded + separator + line;
}).join('\n');
}
// Usage examples:
const code = "function hello() {\n return 'world';\n}";
// Default: 1-based, 3-digit pad, tab separator
addLineNumbers(code, {});
// 001\tfunction hello() {
// 002\t return 'world';
// 003\t}
// Zero-based, no padding, colon separator
addLineNumbers(code, { startNum: 0, padding: 0, separator: ':' });
// 0:function hello() {
// 1: return 'world';
// 2:}
Line Number Format Examples
| Format | Example Output | Use Case |
|---|---|---|
| Plain (no padding) | 1 line one |
Simple lists, notes |
| Zero-padded (3 digit) | 001:line one |
Code, scripts, logs |
| Colon separator | 42:error here |
Error messages, logs |
| Period separator | 1. First item |
Legal documents, lists |
| Tab separator | 001\taligned text |
Monospace alignment |
| Pipe separator | 1 | text |
Markdown, documentation |
Common Use Cases
- Code Review: Add line numbers to code snippets for precise referencing in pull requests and bug reports
- Legal Documents: Number lines in contracts, briefs, and court filings for citation (standard in legal publishing)
- Transcripts: Number deposition, interview, or meeting transcripts for reference
- Log Analysis: Add line numbers to log files for discussing specific entries
- Academic Writing: Number manuscript lines for peer review and editorial comments
- Poetry/Scripts: Number lines in poetry, screenplays, or scripts for analysis
- Documentation: Reference specific configuration lines in technical docs
Line Numbering Standards by Field
| Field | Format | Notes |
|---|---|---|
| Programming | 1: or 1 |
1-based, often colon or no separator |
| Legal (US) | 1 (2-space align) |
Every line numbered, 5-line spacing common |
| Legal (UK) | 1 (margin) |
Numbers in margin, every line |
| Academic | [1] |
Bracketed, for poetry/prose analysis |
| Screenwriting | Scene numbers | Scene headings numbered, not every line |
Line Numbering Examples
Input Text:
function greet(name) {
console.log("Hello, " + name);
return true;
}
Output (default: 3-digit pad, tab separator):
001 function greet(name) {
002 console.log("Hello, " + name);
003 return true;
004 }
Output (colon separator, no padding):
1:function greet(name) {
2: console.log("Hello, " + name);
3: return true;
4:}
Output (period separator, 2-digit pad):
01. function greet(name) {
02. console.log("Hello, " + name);
03. return true;
04. }
Removing Line Numbers
To remove previously added line numbers, use regex replacement:
Regex patterns for removing line numbers: // Remove tab-separated numbers (001\t) Pattern: ^\d+\t Replace: (empty) // Remove colon-separated numbers (1:) Pattern: ^\d+: Replace: (empty) // Remove period-separated with space (1. ) Pattern: ^\d+\. Replace: (empty) // Remove zero-padded with any separator Pattern: ^\d+[\t:.| ]+ Replace: (empty) // Remove bracketed numbers ([1]) Pattern: ^\[\d+\]\s* Replace: (empty) Use the Find and Replace tool with regex mode enabled.
Line Numbering Best Practices
- Consistent padding: Use padding width matching expected max lines (3 digits for <1000 lines)
- Separator choice: Tab for alignment, colon for error-style, period for formal documents
- Starting at 1: Most conventions use 1-based numbering; 0-based is programming-specific
- Preserve indentation: Line numbers should prefix, not replace, existing whitespace
- Empty lines: Decide whether to number empty lines (legal: yes; code: usually yes)
How to Add Line Numbers
- Paste text: Enter or paste the text to number.
- Configure options: Set starting number, padding width, and separator.
- Click Add: Line numbers are prefixed to each line.
- Copy result: Use the numbered text in documents or code.
Tips
- Use monospace font for best visual alignment with tab separators
- 3-digit padding (001-999) works for most code files
- Colon separator mimics editor error messages (file.js:42)
- For legal documents, use period separator with 2-digit padding
- Empty input produces empty output
Frequently Asked Questions
- How does line numbering work?
- Line numbering splits text on newline characters (\n or \r\n), then prefixes each line with an incrementing number. The format is: [number][separator][original line]. Common separators include tab, space, colon, or period. Line numbers typically start at 1, but can be customized for specific use cases.
- What line number formats are supported?
- This tool supports: plain numbers (1, 2, 3), zero-padded numbers (001, 002, 003), and various separators (tab, space, colon, period, pipe). Padding ensures consistent width for sorting and alignment.
- Why use line numbers in code?
- Line numbers enable precise referencing during code review, debugging, and documentation. Instead of 'the function near the top', you can say 'line 47'. Legal documents, court transcripts, and academic texts also use line numbers for citation and cross-reference.
- How do I remove line numbers from text?
- To remove line numbers, use a regex replace: find '^\d+[\t:. ]+' and replace with empty string. For padded numbers, use '^\d+\s+' or '^\d+[\t:. ]'. This tool adds numbers; use find-replace tool with regex to remove them.
- What separator should I use?
- Tab provides best alignment in monospace fonts. Space is simplest for plain text. Colon (:) mimics code editor display (file.js:42). Period (.) is common in legal documents. Choose based on your output format and whether you need visual alignment.
- When should I use zero-padded line numbers?
- Zero-padding (001, 002, ... 100) ensures consistent column width and proper numeric sorting. Use padding when: output will be sorted alphabetically, visual alignment matters, or line count exceeds 99. Padding width should match expected max lines (3 digits for <1000, 4 for <10000).