Alex. S.
Alex. S.

Reputation: 147216

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

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

Upvotes: 837

Views: 430709

Answers (7)

sgargan
sgargan

Reputation: 12628

You can set an environment variable to always have these options apply to all files opened in less:

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

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

Upvotes: 40

vajravelu
vajravelu

Reputation: 93

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

Upvotes: 3

Rob Flickenger
Rob Flickenger

Reputation: 641

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.

Upvotes: 25

Matthew Jaskula
Matthew Jaskula

Reputation: 1306

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.

Upvotes: 40

Daniel Hershcovich
Daniel Hershcovich

Reputation: 3921

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.

Upvotes: 167

dirkgently
dirkgently

Reputation: 111140

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.

Upvotes: 1198

Greg Hewgill
Greg Hewgill

Reputation: 993173

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

Upvotes: 49

Related Questions