727
votes

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

6
less is a linux command line utility, and is very commonly used by programmers to view text files. This question is solidly on-topic for Stack Overflow under the domain of "tools used by programmers" just as all questions relating to using git are on-topic. It is also the first hit in Google when searching for "less show line numbers." This question should not be closed.John Dibling
@JohnDibling The question is more appropriate for Unix & Linux Stack Exchange. Just because less is used by programmers does not make it on topic. Pencils are "tools used by programmers" too but a question about how to sharpen a pencil would not be appropriate here.augurar
@augurar I think you DO know that tools here means software tools. You are deliberately interpreting the meaning of tools out of the context. By the way, I think as well that this question is appropriate for Unix & Linux Stack Exchange, but it does not prevent this question to be here on SO :)Gab是好人

6 Answers

1059
votes

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.

152
votes

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.

47
votes

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
36
votes

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

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

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.

17
votes

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.