Tools / Number each line
Number Each Line
Add a number to the start of every line. Pick where counting starts, what follows the number, and how wide it is.
How it works
Numbering a list by hand is fine for five items and error-prone for fifty. Renumbering after inserting a line in the middle is worse, because every number below shifts. This tool applies a counter to each line, starting wherever you choose, so a list can be renumbered in one pass instead of edited by hand.
The separator is what comes after the number: . for 1. item, ) for 1) item, or leave it empty for a table of bare numbers. Padding sets a fixed width with leading zeros, so 001, 002, 010 line up and sort correctly as text. That matters when the output feeds another tool that sorts alphabetically, because unpadded 10 would sort before 2.
Edge cases
- Padding is for alignment and text sorting, not arithmetic.
001is a label, not the number 1. - A trailing space in the separator is preserved, which is what makes
1. itemread correctly. - Blank lines are numbered too, so the count matches the real line count of the file.
Do it in code instead
The same result with a shell one-liner or a few lines of Python, if you would rather automate it:
Bash (cat)
cat -n list.txt
Bash (awk, custom)
awk '{ printf "%03d. %s\\n", NR, $0 }' list.txt
Python
for i, l in enumerate(open('list.txt').read().splitlines(), start=1):
print(f'{i:03d}. {l}')
JavaScript
text.split('\n').map((l,i) => `${i+1}. ${l}`).join('\n');
Questions
- How do I start numbering at zero?
- Set “start at” to 0. The counter increments per line, so the first line is 0 and the second is 1. Any integer is accepted.
- What does padding do?
- It adds leading zeros until the number reaches the width you set. With a width of 3, line 7 becomes 007. This keeps a column aligned and lets text sorting produce the same order as numeric sorting.
- Can I number only non-empty lines?
- Every line is numbered, including blank ones, so the numbers match physical line numbers. Remove empty lines first if you want a compact numbered list instead.