Add Line Numbers to a Text File – Easy Ways
In this article
Yes, you can easily add line numbers to a text file using built-in tools in most operating systems, or with a quick script. This guide covers the simplest methods for Windows, macOS, and Linux, plus how to do it in common text editors.
Why Add Line Numbers?
Line numbers are essential for referencing specific parts of a file, especially in programming, scripting, and data processing. They make it easier to discuss errors, compare versions, or debug code. While most code editors show line numbers by default, you might need to add them permanently to a plain text file for printing, sharing, or further processing.
Using Built-in Command Line Tools
The most efficient way to add line numbers is with command-line utilities. These are available on all major operating systems without installing anything extra.
On Linux and macOS (using nl or cat)
- Open a terminal.
- Use the
nlcommand:nl -ba input.txt > output.txt. The-baflag numbers all lines, including blank ones. - Alternatively, use
cat -n input.txt > output.txt. This also numbers all lines but adds a tab between the number and the text.
On Windows (using PowerShell)
- Open PowerShell.
- Use the following command:
$i=0; Get-Content input.txt | ForEach-Object { $i++; "$i`t$_" } | Set-Content output.txt. This numbers each line and saves the result to a new file.
Using a Text Editor
If you prefer a graphical interface, many text editors can add line numbers directly.
- Notepad++ (Windows): Go to Edit > Column Editor, choose “Number to Insert”, set starting number and increment, then click OK. This inserts numbers at the cursor position.
- VS Code: While it doesn't have a direct “insert line numbers” command, you can use the extension “Insert Line Numbers” or run a simple find-and-replace with a regex to prepend numbers.
- Sublime Text: Use the “InsertLineNumbers” package or a similar plugin.
Adding Line Numbers with a Script
For more control, a short script can be useful, especially if you need to format the numbers (e.g., with leading zeros).
Python (cross-platform)
with open('input.txt') as f:
lines = f.readlines()
with open('output.txt', 'w') as f:
for i, line in enumerate(lines, 1):
f.write(f"{i:4d} {line}")This script reads input.txt, adds a 4-digit right-aligned number, and writes to output.txt. You can adjust the format easily.
How to Choose the Right Method
The best method depends on your situation:
- One-time task: Use command-line tools (
nlor PowerShell) – they are fast and require no setup. - Editing a file interactively: Use a text editor with column editing, like Notepad++.
- Automated or repeated task: Write a small script (Python, Bash, etc.) to integrate into your workflow.
- Need to preserve formatting: Be careful with tabs vs. spaces. Command-line tools often use tabs, which may not align perfectly in all viewers.
Common Problems and Solutions
- Blank lines not numbered: By default,
nlskips blank lines. Use-bato include them. - Numbers not aligned: Use a fixed-width format in your script (e.g.,
{:4d}in Python) to align numbers. - Windows line endings (CRLF): When using PowerShell,
Set-Contentadds a BOM and uses CRLF. If you need LF, use-Encoding UTF8NoBOMor a script. - Large files: For very large files, command-line tools are more memory-efficient than opening in an editor.
Comparing Methods
| Method | Speed | Ease | Flexibility |
|---|---|---|---|
| Command line (nl) | Fast | Easy | Limited formatting |
| PowerShell | Moderate | Moderate | Good |
| Text editor | Slow for large files | Easy | Good |
| Script (Python) | Fast | Requires coding | High |
Cost and Safety
All methods described are free and use built-in tools or open-source software. They are safe – they don't modify the original file unless you overwrite it. Always test on a copy if you're unsure. For sensitive data, be aware that command-line commands may be logged; use a local script if privacy is a concern.
Frequently Asked Questions
How do I add line numbers to a text file in Windows without installing anything?
Use PowerShell as described above. It's built into Windows 10 and 11. The command is a bit long, but you can save it as a .ps1 script for reuse.
Can I add line numbers to a PDF?
Yes, but it's more complex. You'd need to convert the PDF to text, add numbers, then convert back to PDF using a tool like Adobe Acrobat or a free online converter. Alternatively, print with line numbers from a text editor that supports it.
Why does the nl command not number blank lines?
By default, nl treats blank lines as logical page breaks. Use the -ba option to number every line, including blanks.
How do I add line numbers to a file in Notepad++?
Open the file, place the cursor at the start of the first line, go to Edit > Column Editor, select “Number to Insert”, set initial number and increment, then click OK. This will insert numbers at the cursor position for all selected lines.
Is there a way to add line numbers to a file in Google Docs?
Google Docs doesn't have a built-in line numbering feature. You can add line numbers manually in a table or use an add-on, but it's not straightforward. For coding, use a dedicated editor.