795

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

3
  • 95
    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. Jun 7, 2013 at 11:37
  • 5
    @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
    Feb 18, 2015 at 0:01
  • 26
    @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 :) Apr 26, 2016 at 16:53

6 Answers 6

1128

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.

10
  • 7
    When I less a huge file then "G" to the bottom, it says "Calculating line numbers... (interrupt to abort)" even though it is not displaying line numbers. I'd like to know how to find out what line I'm on without exiting and relaunching with -N. I'm suffering the penalty. Where's the reward? Aug 4, 2009 at 16:23
  • 109
    wow, I have looked at the help for less dozens of times hoping to find the answer to this. I have always overlooked "-<flag> Toggle a command line option [see OPTIONS below]." ...which means you can type "-N" from within less. I have been annoyed by this for about a decade. Good times! Aug 4, 2009 at 16:33
  • 2
    is it possible to display the line numbers in a different color?
    – greg
    Jul 2, 2013 at 8:24
  • 3
    Hi @greg You can use sed to colorize the line numbers and option -R to let less display colors: cat -n file.txt | sed 's/^[ 0-9]*[0-9]/\o033[34m&\o033[0m/' | less -R You may also customize LESSOPEN... Cheers ;)
    – oHo
    Aug 28, 2013 at 11:11
  • 1
    Thanks for the Holistic approach, @dirkgently!
    – elena
    Dec 31, 2018 at 10:07
158

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.

49

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
2
  • 6
    Which version(s) of less are you aware of that do(es) not support it? Aug 22, 2017 at 21:01
  • 1
    I usually use nl instead of cat -n
    – ychaouche
    May 27, 2018 at 12:55
37

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

export LESS='-RS#3NM~g'
5
  • 2
    Line alias less="LESS='-RS#3NM~g' less" in .bashrc did my day! Thanks!
    – Jury
    Sep 26, 2016 at 13:41
  • 8
    An explanation of this incantation would be very much welcome :)
    – minexew
    Aug 28, 2018 at 15:27
  • 9
    The options are: R = better handling of raw color codes in files. S = Scroll long lines off the screen instead of word wrap. #3 = scroll right/left by 3 positions at a time. N = show line numbers. M = Longer prompts. ~ = Instead of displaying empty space after a file ends with ~, display nothing for blank space. g = when doing a search with 'g', only highlight the current match instead of all matches.
    – Bryan
    Feb 19, 2019 at 23:18
  • This also assigns line number to man is it possible to prevent it and keep it only for files?
    – alper
    Jul 30, 2020 at 20:37
  • I guess by aliasing the commands that have the trouble, or more simply less to set the variable only for each of its invocations.
    – Pysis
    Dec 29, 2021 at 17:08
36

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.

1
  • 1
    Passing -N or --LINE-NUMBERS only shows the date for me in CentOS 5.3. However using -N after starting less works fine. Mar 22, 2010 at 14:19
21

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.