Add Line Numbers to Text
Number each line of your text automatically with our line numbering tool. Perfect for code documentation, legal documents, scripts, and any content where line references are needed for review or discussion.
Line Numbering Formats
| Format | Example | Common Use | Simple1. Line contentBasic numbering Padded001. Line contentConsistent alignment Bracketed[1] Line contentCode comments Tab-separated1\tLine contentData processing | Custom | Line 1: content | Flexible formatting |
Line Numbering Implementation
``javascript
function addLineNumbers(text, options = {}) {
const {
startAt = 1,
separator = '. ',
padWidth = 0,
prefix = '',
suffix = ''
} = options;
const lines = text.split('\n');
const totalDigits = padWidth || String(lines.length + startAt - 1).length;
const numberedLines = lines.map((line, index) => {
const lineNum = index + startAt;
const paddedNum = String(lineNum).padStart(totalDigits, '0');
return ${prefix}${paddedNum}${separator}${line}${suffix};
});
return {
lineCount: lines.length,
format: ${prefix}N${separator}content${suffix},
result: numberedLines.join('\n')
};
}
// Example output:
// 01. First line
// 02. Second line
// 03. Third line
``
Use Cases for Line Numbers
Code review: "See the bug on line 47" - line numbers enable precise references. Legal documents: Contracts and depositions use line numbers for citations. Script formats: Screenplays and stage directions use line numbers for rehearsal references. Educational materials: Line-numbered poems and texts aid literary analysis.
Formatting Options
Zero-padding ensures consistent alignment for documents with many lines: "001" through "999" align perfectly. Choose separators (period, colon, tab) based on your document format. For code, consider formats like "// Line 42:" that work as comments.