GNU
Less Command
Line Numbers
Unix/Linux
Terminal Commands

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.

bash
1# Open a file with line numbers
2less -N /var/log/syslog
3
4# Combine with other flags
5less -NS /etc/nginx/nginx.conf    # -S disables line wrapping
6less -NM /var/log/auth.log        # -M shows a more detailed prompt
7less -Ni /etc/hosts               # -i enables case-insensitive search

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.

text
1# While viewing a file in less:
2
3-N        then press Enter    → Toggle line numbers on/off
4-n        then press Enter    → Same toggle (alternative key)
5
6# To jump to a specific line number:
750g       → Jump to line 50
8100G      → Jump to line 100 (capital G also works)
9
10# To see your current position:
11=         → Shows current line range, byte position, and percentage

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.

bash
1# For bash (~/.bashrc or ~/.bash_profile)
2export LESS='-N'
3
4# For zsh (~/.zshrc)
5export LESS='-N'
6
7# Combine multiple default options
8export LESS='-NRiMS'
9# -N  line numbers
10# -R  interpret ANSI color codes (essential for colored output like git diff)
11# -i  case-insensitive search (unless search contains uppercase)
12# -M  more verbose prompt
13# -S  disable line wrapping (scroll horizontally instead)
14
15# Apply without restarting the terminal
16source ~/.bashrc

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:

bash
1# Override the LESS variable for a single invocation
2LESS='' less file.txt
3
4# Or pass +N to turn off line numbers explicitly
5less +N file.txt

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.

CommandAction
Ng or NGJump to line N (e.g., 42g jumps to line 42)
g or 1GJump to the beginning of the file
GJump to the end of the file
/patternSearch forward for a pattern
?patternSearch backward for a pattern
nRepeat previous search forward
N (capital)Repeat previous search backward
=Show current line number, byte offset, and file percentage
FFollow mode (like tail -f), press Ctrl+C to stop
&patternShow only lines matching the pattern

Jumping to a Line from the Command Line

You can open less at a specific line number directly:

bash
1# Open file at line 150
2less +150 file.txt
3
4# Open file at line 150 with line numbers displayed
5less -N +150 file.txt
6
7# Open file at the first occurrence of a pattern
8less +/ERROR /var/log/syslog

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.

bash
1# Git log with line numbers
2git log | less -N
3
4# Man pages with line numbers
5man bash | less -N
6
7# Viewing a large JSON file with jq and line numbers
8jq '.' large-file.json | less -N
9
10# Viewing compiler output
11make 2>&1 | less -N

Configuring less as the Default Pager

bash
1# Set less as the default pager with your preferred options
2export PAGER='less -NR'
3
4# Git-specific pager configuration
5git config --global core.pager 'less -NR'
6
7# Man-specific pager
8export MANPAGER='less -NiRS'

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:

bash
1# Show line numbers only in the prompt (no column in the text)
2less -M file.txt
3# The prompt at the bottom shows "lines X-Y/TOTAL"
4
5# Use cat -n to add line numbers to the text itself
6cat -n file.txt | less
7# Line numbers are part of the text, searchable with /
8
9# Use nl for more control over number formatting
10nl -ba file.txt | less
11# -ba numbers all lines including blank ones
12
13# Use grep -n to show line numbers for matching lines
14grep -n "pattern" file.txt | less
MethodLine numbers in textScrollable positionWorks with search
less -NYes (separate column)Updates dynamicallySearch ignores line numbers
less -MNo (prompt only)Shows in status barN/A
cat -n | lessYes (part of text)StaticSearch includes line numbers
nl | lessYes (part of text)StaticSearch 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) with N (repeat search backward). The -N is typed at the less command prompt with a leading dash. Capital N without a dash repeats the last search in the reverse direction. These are completely different operations.
  • Setting LESS='-N' and getting unexpected behavior in git diff. Git diff output is paged through less. With -N, every diff line gets a line number, which can be confusing because diff already has its own line numbering. Use git 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 less output to another command (e.g., less -N file.txt | grep something), line numbers are not included because less only adds them for display, not to the output stream.
  • Not using -R alongside -N for colored content. Without -R, ANSI color escape codes appear as raw text (e.g., ^[[32m). Always combine -N with -R when viewing colored output: less -NR.
  • Forgetting that +N disables line numbers. The + prefix inverts the flag. If your LESS environment variable includes -N and you open a file with less +N file.txt, line numbers will be off. This is intentional but can be confusing.
  • Using lesskey configuration that conflicts with -N. Custom lesskey bindings can remap the -N toggle. If -N does not work as expected inside less, check ~/.lesskey or ~/.config/lesskey for 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.


Course illustration
Course illustration

All Rights Reserved.