How to display line numbers in 'less' (GNU)

UnixGnu

Unix Problem Overview


What is the command to make less display line numbers in the left column?

Unix Solutions


Solution 1 - Unix

From the manual:

> -N or --LINE-NUMBERS Causes a line number to be displayed at the beginning of each line in the display.

You can also toggle line numbers without quitting less by typing -N.

It is possible to toggle any of less's command line options in this way.

Solution 2 - Unix

You can also press = while less is open to just display (at the bottom of the screen) information about the current screen, including line numbers, with format:

myfile.txt lines 20530-20585/1816468 byte 1098945/116097872 1%  (press RETURN)

So here for example, the screen was currently showing lines 20530-20585, and the files has a total of 1816468 lines.

Solution 3 - Unix

You could filter the file through cat -n before piping to less:

cat -n file.txt | less

Or, if your version of less supports it, the -N option:

less -N file.txt

Solution 4 - Unix

You can set an enviroment variable to always have these options apply to all less'd file:

export LESS='-RS#3NM~g'

Solution 5 - Unix

The command line flags -N or --LINE-NUMBERS causes a line number to be displayed at the beginning of each line in the display.

You can also toggle line numbers without quitting less by typing -N<return>. It it possible to toggle any of less's command line options in this way.

Solution 6 - Unix

If you hit = and expect to see line numbers, but only see byte counts, then line numbers are turned off. Hit -n to turn them on, and make sure $LESS doesn't include 'n'.

Turning off line numbers by default (for example, setting LESS=n) speeds up searches in very large files. It is handy if you frequently search through big files, but don't usually care which line you're on.

I typically run with LESS=RSXin (escape codes enabled, long lines chopped, don't clear the screen on exit, ignore case on all lower case searches, and no line number counting by default) and only use -n or -S from inside less as needed.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlex. S.View Question on Stackoverflow
Solution 1 - UnixdirkgentlyView Answer on Stackoverflow
Solution 2 - UnixDaniel HershcovichView Answer on Stackoverflow
Solution 3 - UnixGreg HewgillView Answer on Stackoverflow
Solution 4 - UnixsgarganView Answer on Stackoverflow
Solution 5 - UnixMatthew JaskulaView Answer on Stackoverflow
Solution 6 - UnixRob FlickengerView Answer on Stackoverflow