802

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alex. S.
  • 143,260
  • 19
  • 55
  • 62
  • 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. – John Dibling Jun 07 '13 at 11:37
  • 5
    @JohnDibling The question is more appropriate for [Unix & Linux Stack Exchange](https://unix.stackexchange.com/). 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 '15 at 00: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 :) – Gab是好人 Apr 26 '16 at 16:53

7 Answers7

1137

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.

Luke Peterson
  • 8,584
  • 8
  • 45
  • 46
dirkgently
  • 108,024
  • 16
  • 131
  • 187
  • 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? – Bruno Bronosky Aug 04 '09 at 16:23
  • 1
    Hit ENTER/RETURN if you're trying to toggle. After typing -n or -N while using less, you may also need to hit that afterwards to put the changes into effect. It even says so at the bottom, but my brain didn't connect the dots because I assumed it would be instantaneous and I kept typing -N and was wondering why the numbers never showed up. – abelito Mar 27 '19 at 10:57
  • 2
    Note that `less -N ` works but not `less -N`. – 林果皞 Jun 12 '19 at 21:02
  • 1
    YES!! FYI: If you use `-h` from within `less` as guidance, ***it will lead you astray***. It lists `-n` and `-N` as equivalent, **but they are not**. `-N` works, `-n` doesn't. Is this a bug - or am I missing something? –  Feb 14 '21 at 22:36
  • 1
    @BrunoBronosky You can hit the = key at any time while in less, it will show you the line range currently being displayed plus the total size (available for files, not streams) like this at the bottom: "Filename.txt lines 1-50/300 byte 4021/26976 15% (press RETURN)" – Razzle Aug 10 '21 at 08:29
  • @Seamus `-n` toggles the line numbers you see on the status line shown with `Ctrl-g`. `-N` toggles line numbers on each individual line. I agree with you in that the help text should be more clear about this. – Snake Jul 15 '22 at 05:08
159

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.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Daniel Hershcovich
  • 3,751
  • 2
  • 30
  • 35
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
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
37

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

export LESS='-RS#3NM~g'
Marek Grzenkowicz
  • 17,024
  • 9
  • 81
  • 111
sgargan
  • 12,208
  • 9
  • 32
  • 38
  • 2
    Line `alias less="LESS='-RS#3NM~g' less"` in `.bashrc` did my day! Thanks! – Jury Sep 26 '16 at 13:41
  • 8
    An explanation of this incantation would be very much welcome :) – minexew Aug 28 '18 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 '19 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 '20 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 '21 at 17:08
37

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matthew Jaskula
  • 1,266
  • 1
  • 16
  • 16
  • 1
    Passing -N or --LINE-NUMBERS only shows the date for me in CentOS 5.3. However using -N after starting less works fine. – Mike Miller Mar 22 '10 at 14:19
22

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rob Flickenger
  • 551
  • 5
  • 6
0

the above answers shows how to initiate a less with line numbers enabled, but for those who want to know how to toggle line numbering ON and OFF in the file already being viewed with less

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

Courtesy: lostsaloon

vajravelu
  • 53
  • 1
  • 8