Open a Text File at a Specific Line Number
In this article
Yes, you can open a text file directly at a specific line number in most text editors and command-line tools. The exact method depends on your operating system and which editor you use, but the most common approaches are the +N flag in Vim, Ctrl+G in Notepad++ (after opening the file), and the sed command in Linux/macOS terminals.
Why Open at a Specific Line?
When you're debugging code, parsing logs, or reviewing a large configuration file, scrolling through thousands of lines wastes time and risks losing your place. Jumping straight to a line number lets you:
- Inspect an error message that points to line 1,234 without manually scrolling.
- Compare two sections of a file side by side.
- Edit a specific entry in a CSV or JSON file without loading the whole thing into memory.
Most editors and command-line tools support this, but the syntax varies. Knowing the right command for your environment saves minutes on every large-file session.
Command-Line Tools (Linux, macOS, Windows Terminal)
If you work in a terminal, these are the fastest methods:
- Vim –
vim +123 filename.txtopens the file with the cursor on line 123. You can also usevim +/pattern filenameto jump to the first line containing a pattern. - Nano –
nano +123 filename.txtopens the file at line 123. (Note: some older versions may not support this; check your man page.) - sed –
sed -n '123p' filename.txtprints only line 123. To see a range, usesed -n '123,130p'. - awk –
awk 'NR==123' filename.txtprints line 123. This is useful if you want to process the line further. - less –
less +123 filename.txtopens the file and positions the view at line 123. You can then navigate normally.
On Windows, if you have more or PowerShell, the options are limited. PowerShell's Get-Content can read a specific line but doesn't open an editor at that line. For editing, you'd typically use a graphical editor (see next section).
Graphical Text Editors
Most GUI editors have a "Go to Line" dialog. Here are the common shortcuts:
- Notepad++ – Press
Ctrl+G, type the line number, and press Enter. The view jumps immediately. - VS Code – Press
Ctrl+G(Windows/Linux) orCmd+G(macOS), type the line number, and press Enter. You can also useCtrl+Pand type:123to jump. - Sublime Text – Press
Ctrl+G(orCmd+Gon Mac), enter the line number, and hit Enter. - gedit – Press
Ctrl+Ito open the "Go to Line" dialog. - Atom – Press
Ctrl+G, type the number, and press Enter.
In all these editors, the dialog usually shows the current line and total lines, which helps you verify the number before jumping.
How to Find the Line Number You Need
Before you can jump to a line, you need to know the number. Common ways to find it:
- In the editor – Most editors show the current line number in the status bar (bottom right). If not, enable line numbers in the view settings.
- From an error message – Compilers and interpreters often report errors as
filename.py:123: SyntaxError. That number is the line you need. - Using grep – grep -n "pattern" filename prints lines with their numbers. For example,
- Using
nl–nl -ba filenamedisplays the file with line numbers in the terminal.
If you're in a terminal and want to jump to a line number that you just found via grep, you can combine them: vim +$(grep -n "pattern" file | cut -d: -f1) file. This opens Vim at the line where the pattern first appears.
Common Problems and Misconceptions
One frequent mistake is confusing line numbers with byte offsets. In text files, a line is not a fixed number of bytes; it's a sequence of characters ending with a newline. So you can't reliably jump to a line by seeking to a byte position unless you know the exact encoding and line endings.
Another issue: some editors (like older versions of Notepad) don't support jumping to a line at all. If you're stuck with a basic editor, you can use a command-line tool to extract the line and then open it, but that's not the same as editing in context.
Also, be aware that line numbering can differ between editors if the file uses unusual line endings (e.g., old Mac \r instead of \n). Most modern editors handle this automatically, but if you see unexpected line numbers, check the file's line-ending style.
Alternatives to Opening at a Line
Sometimes you don't need to open the file at all. If you only need to view a specific line, use sed or awk to print it to the terminal. If you need to edit a line, you can use sed -i '123s/old/new/' file to replace text on that line in-place (on Linux/macOS). For more complex edits, tools like awk or Perl one-liners can modify specific lines without opening an editor.
For very large files (gigabytes), opening an editor can be slow or impossible. In that case, streaming tools like sed, awk, or tail/head are more efficient. For example, tail -n +123 file | head -n 1 prints line 123 without loading the whole file.
Frequently Asked Questions
How do I open a file at a specific line in Vim?
Use the + flag: vim +123 filename. This places the cursor on line 123. You can also use vim +/search_term filename to jump to the first line containing that term.
Can I open a file at a line in Notepad++?
Yes. Open the file normally, then press Ctrl+G. A dialog appears where you type the line number and press Enter. The view jumps to that line.
Is there a way to open a file at a line in VS Code from the command line?
Yes. Use code -g filename:123 to open VS Code at line 123 of that file. This works on Windows, macOS, and Linux if the code command is in your PATH.
What does sed -n '123p' do?
It prints line 123 of the file to the terminal. The -n suppresses automatic printing, and 123p tells sed to print only that line. This is a quick way to view a line without opening an editor.
How do I jump to a line in a file opened in less?
Start less with less +123 filename. Once inside less, you can also press g followed by the line number and Enter to jump to that line.
Why does my editor show a different line number than the terminal?
This usually happens when the file uses mixed line endings (some \n, some \r\n) or when the editor counts wrapped lines as multiple lines. Check your editor's line-ending settings and disable line wrapping if you need exact line numbers.