33

As the title implies, when I am browsing text file,I would like to know the current viewed porportion of the whole text file.

I know that with -N option,we could turn on the line numbers,but how could I make less display the line number of the whole text file?

thanks.

Jichao
  • 7,940

6 Answers6

35

If you open a file with less then pressing Ctrl-g will display the current line number, total lines and percentage as well as shown below:

lines 51-100/185 byte 3228/5886 54% (press RETURN)

25

Do you mean like with the -M switch?

Teddy
  • 7,218
7

Just expanding a wee bit on the previous answers. Command line:

less -M +Gg

does the following:

-M Show current position within the file on the prompt +Gg Run commands G (go to the end of the file) and g (go back to the beginning of the file)

If less is reading from stdin, which happens when man is showing a man page, it doesn't know the total number of lines in the file in advance, so that it can properly calculate its position. Therefore, +Gg is necessary so that less can get the total number of lines, and thus calculate the current position as a percentage.

I found it useful to set these variables in ~/.bashrc:

export LESS+='-M'
export MANPAGER='less +Gg'

LESS will ensure that all invocations of less will show the current position on the prompt, and MANPAGER will ensure that less will be able to get the total number of lines in the man page, which less will then show on the prompt. It seemed to be more sensible not to include +Gg in LESS to prevent less from trying to go to the end of potentially large piped input. If that's necessary, one can always run the commands G and g manually.

6

less -M +Gg

for not very big files it's OK, because +G (G command) go till the end of file

ALZ
  • 417
1

Or you could use moar, which does what you want out of the box: https://github.com/walles/moar

moar is designed to be simple to switch to from less.

moar also comes with syntax highlighting input by default, nice search, word wrapping and lots of other things missing from less.

Screenshot showing line numbers

Disclaimer: I wrote moar so of course I like it!

0

ctrl-g works, just press = will do the same and quicker.

@Jichao has mentioned this, just post this answer for others to find easyer.

by the way, including the man page part

= or ^G or :f

Prints some information about the file being viewed, including its name and the line number and byte offset of the bottom line being displayed. If possible, it also prints the length of the file, the number of lines in the file and the percent of the file above the last displayed line.