Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

unix

gnu

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

like image 610
Alex. S. Avatar asked May 06 '09 20:05

Alex. S.


4 Answers

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.

like image 96
dirkgently Avatar answered Oct 12 '22 14:10

dirkgently


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.

like image 35
Daniel Hershcovich Avatar answered Oct 12 '22 15:10

Daniel Hershcovich


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
like image 49
Greg Hewgill Avatar answered Oct 12 '22 14:10

Greg Hewgill


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

export LESS='-RS#3NM~g'
like image 37
sgargan Avatar answered Oct 12 '22 14:10

sgargan