how to show line numbers in the “less” command line file viewer

No matter which file viewer you are using, you more often than not would want to show line numbers along with the contents of the file. The less file viewer is no different. First, let me describe what the less utility does, before I tell you how to show line numbers in less.

The less is a command line utility that allows you to view text file content easily right from the command line. It is similar to the more utility with some additional functionalities. The main difference between the two utilities lies in its scrolling capabilities: more allows you to scroll forward and view pages or a screen full of content, while less allows you to scroll both forward and backwards while viewing content as pages.

The less utility does not show line numbers of the lines by default when viewing the file content. Quite often it is informative to display the line numbers while viewing the file. You have several different options if you want to show line numbers with the less utility.

Command Line Option

If you are typing in the less command from the command prompt, then you can easily use the command line argument -N or –LINE-NUMBERS to show line numbers at the beginning of each line on the display.

bash$ less --LINE-NUMBERS filename.txt

Or you can use the short code or abbreviation -N with the less command as in the example below.

bash$ less -N filetwo.txt

Of course, you will need to remember to use the command line option as you are typing in the command. Not a big deal most of the time, but you do have other options if you forget. You can show line numbers and toggle it from inside the viewer as well.

Inside the Viewer

You can also toggle the line numbers from inside the less viewer, as you are viewing the file content. This is useful if you are already inside the viewer or if you want to remove the line number display. As with most command line options of less, you can also use it from with in the viewer…

When the file content is being displayed, just type -N using the keyboard and followed by Enter to display line numbers. You can hide the line numbers by typing -N (or -n) again followed by Enter from with in the viewer. This is a quick way to toggle line numbers and much more convenient than the command line option.

less show line numbers

Changing the Default

If you want to always show line numbers when using less, then you can modify your environment in order to do so. There are a couple of ways to do this: by using a command alias or by modifying the environment variable.

You can create an alias for the less command such that the -N option is always passed on to the less command. In bash, you can create a new alias as shown below. The syntax might vary slightly depending on the shell you are using

bash$ alias less="less -N"

Another option is to create or modify the environment variable called LESS. Whatever options you set in this environment variable will be used as the default by the less utility.

bash$ export LESS="-N";

Of course, you can now add these to your command configuration files such as .bashrc or .cshrc or .bash_profile etc to make this change permanent and persist it over shutdowns and reboots.