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.
When working with large files in a Unix-like environment, the less command is an invaluable tool, allowing users to view contents of a file one screen at a time. By default, less does not show line numbers, but they can be extremely useful, especially when reviewing or debugging code. This article will guide you through several methods to display line numbers in less.
Command Line Option
The simplest way to start less with line numbers is by using the -N command line option. For example:
This command will open file.txt in less, displaying line numbers at the beginning of each line. The numbers are displayed in a separate column to the left of the text.
Inside less Commands
If you have already opened a file with less and decide you want to toggle line numbers, you can do so without exiting. The command to toggle line numbers within less is:
To use this, while less is open, simply type -N and press Enter. The line numbers will appear or disappear depending on their previous state.
Using lesskey to Setup Key Bindings
For users who frequently need line numbers, setting up a key binding with lesskey can be very efficient. lesskey is a program that allows you to customize the key bindings for less. You can create a lesskey file that includes a key binding for toggling line numbers.
- Create a new file named
.lesskeyin your home directory. - Add the following lines to this file:
- After saving the file, you compile it by running the
lesskeycommand, which will generate a binary file with your key settings. - Now, whenever you are using
less, you can toggle line numbers on and off by simply typing-N.
Permanent Setting in Shell Configuration
If you prefer having line numbers displayed by default every time you use less, you can set an alias in your shell configuration file (e.g., .bashrc, .zshrc).
Add the following line to your configuration file:
After adding this line, either restart your terminal session or source the configuration file (e.g., source ~/.bashrc). Now, every time you use less, it will automatically display line numbers.
Advantages of Using Line Numbers
Line numbers are particularly useful when:
- Debugging code: Refer to specific lines in error messages.
- Collaborating: Discuss specific parts of file content with peers.
- Navigating: Jump to specific lines quickly using the
Gcommand inless.
Summary Table
| Feature | Command | Description |
| Toggle Line Numbers | -N inside less | Toggles display of line numbers on and off. |
| Enable Line Numbers | less -N filename | Starts less with line numbers enabled. |
| Permanent Enabling | alias less='less -N' | Always opens less with line numbers enabled. |
In conclusion, displaying line numbers in less can significantly enhance the usability of this tool for reviewing and editing texts, codes, or any lengthy documents. Whether enabling it per-use, toggling during runtime, or enforcing it through shell settings, line numbers can provide essential navigational and developmental support.

