28

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,145
  • 12
  • 52
  • 62

4 Answers4

29

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)

secureBadshah
  • 1,611
  • 13
  • 10
  • 1
    thanks.after revisiting the man page,manage to find it and get the other two equal command ':f' and '='.sorry for this. – Jichao Nov 03 '09 at 13:29
  • 1
    Unfortunately it does not work through pipes, neither with the -M or -m parameter from the other answer. For example `wdiff version1.txt version2.txt | colordiff | less -RM` does not show the percentage. Let me know if someone has a solution or workaround for this. Update: the solution is to add +Gg like another answer explained. – baptx Jun 05 '19 at 14:51
24

Do you mean like with the -M switch?

Teddy
  • 6,848
  • 4
  • 19
  • 19
  • That's exactly what I want! – Jichao Nov 03 '09 at 13:31
  • Or `-m` if line-number is not necessary. – Franklin Yu Oct 25 '17 at 15:26
  • 2
    Do you have an idea if there is a solution or workaround to display the percentage when using pipes? For example I cannot see the percentage for a colored diff with this command: `wdiff version1.txt version2.txt | colordiff | less -RM` Update: the solution is to add +Gg like another answer explained. – baptx Jun 05 '19 at 14:48
6

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
  • 377
  • 2
  • 4
  • 9