How to display line numbers in 'less' (GNU)
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
To display line numbers in less, pass the -N flag when opening a file: less -N file.txt. If you have already opened a file in less, type -N followed by Enter to toggle line numbers on or off. For a permanent default, set the LESS environment variable: export LESS='-N' in your shell profile.
Using the -N Flag
The -N option adds a line number column to the left of every line. Numbers update dynamically as you scroll, and they do not become part of the text if you pipe the output elsewhere.
The line number column is typically 7 characters wide. On narrow terminal windows, this reduces the visible text width. If that is a concern, toggle line numbers on only when you need them.
Toggling Line Numbers Inside less
If you opened a file without -N and realize you need line numbers, you do not need to exit and reopen the file.
The toggle is immediate. Pressing -N once turns line numbers on; pressing it again turns them off. This is useful when you want line numbers temporarily for referencing a specific line in a code review or bug report.
Making Line Numbers the Default
If you always want line numbers, set the LESS environment variable in your shell configuration file.
With LESS set, every invocation of less (including those triggered by man, git log, git diff, and other tools that use less as a pager) will include line numbers by default.
Overriding the Default for a Single Command
If you have set -N as the default but want to suppress it for one command:
The + prefix inverts a flag. +N disables line numbers regardless of the LESS environment variable.
Useful Navigation Commands
Line numbers become more powerful when combined with navigation commands.
| Command | Action |
Ng or NG | Jump to line N (e.g., 42g jumps to line 42) |
g or 1G | Jump to the beginning of the file |
G | Jump to the end of the file |
/pattern | Search forward for a pattern |
?pattern | Search backward for a pattern |
n | Repeat previous search forward |
N (capital) | Repeat previous search backward |
= | Show current line number, byte offset, and file percentage |
F | Follow mode (like tail -f), press Ctrl+C to stop |
&pattern | Show only lines matching the pattern |
Jumping to a Line from the Command Line
You can open less at a specific line number directly:
This is particularly useful when a compiler error or stack trace references a specific line number and you want to navigate there immediately.
Using less as a Pager for Other Tools
Many command-line tools use less as their default pager. Line numbers can be especially helpful in these contexts.
Configuring less as the Default Pager
Alternatives to -N for Line Context
Sometimes you do not need permanent line numbers but want to see which line you are on. These alternatives may be sufficient:
| Method | Line numbers in text | Scrollable position | Works with search |
less -N | Yes (separate column) | Updates dynamically | Search ignores line numbers |
less -M | No (prompt only) | Shows in status bar | N/A |
cat -n | less | Yes (part of text) | Static | Search includes line numbers |
nl | less | Yes (part of text) | Static | Search includes line numbers |
The key advantage of less -N over piping through cat -n is that less -N shows true line numbers that update as you scroll, and the numbers are not part of the searchable text. With cat -n, the numbers are baked into the content, which means searching for 42 would match line number 42, line 142, and any content containing "42."
Common Pitfalls
- Confusing
-N(line numbers) withN(repeat search backward). The-Nis typed at the less command prompt with a leading dash. CapitalNwithout a dash repeats the last search in the reverse direction. These are completely different operations. - Setting
LESS='-N'and getting unexpected behavior ingit diff. Git diff output is paged throughless. With-N, every diff line gets a line number, which can be confusing because diff already has its own line numbering. Usegit config --global core.pager 'less -R'to set a Git-specific pager without-N. - Expecting line numbers to appear in piped output. When you pipe
lessoutput to another command (e.g.,less -N file.txt | grep something), line numbers are not included becauselessonly adds them for display, not to the output stream. - Not using
-Ralongside-Nfor colored content. Without-R, ANSI color escape codes appear as raw text (e.g.,^[[32m). Always combine-Nwith-Rwhen viewing colored output:less -NR. - Forgetting that
+Ndisables line numbers. The+prefix inverts the flag. If yourLESSenvironment variable includes-Nand you open a file withless +N file.txt, line numbers will be off. This is intentional but can be confusing. - Using
lesskeyconfiguration that conflicts with-N. Customlesskeybindings can remap the-Ntoggle. If-Ndoes not work as expected insideless, check~/.lesskeyor~/.config/lesskeyfor conflicting bindings.
Summary
Display line numbers in less with less -N filename, toggle them inside an open session by typing -N and pressing Enter, or make them permanent by adding export LESS='-N' to your shell profile. Combine with -R for colored output, -S for no-wrap mode, and -M for verbose prompts. Use Ng to jump to line N, = to check your current position, and +/pattern to open at a search hit. For Git and other tools that use less as a pager, configure the pager separately to avoid line numbers cluttering diff output. The -N flag adds display-only line numbers that do not affect the text content, searches, or piped output.

