How to make Less indicate location in percentage
export LESS="-m"
More generally, the LESS
environment variable may contain options equivalent to command line flags you could explicitly pass when running less
-- here, the -m
option that tells it to prompt more richly (including the percentage, as you asked). You could pass also more than one option within that single environment variable by ending each with a $
. For much more info, see less's manpage.
Edit: it is of course possible (depending on how you're using less, e.g. if you're piping to it rather than calling it on a file) that less doesn't know the total size it will be displaying, in which case of course it can't show the % -- in that case it will prompt with what little info it does have, e.g., how much text has it shown so far. For example, man
does use less
that way, by piping.
So, if your specific need is to see the % in man
(rather than when calling less
directly on a file) you need to use an "alternate pager" (environment variable MANPAGER
or switch -P
on the man
command line) which is a simple script that saves man
's output to a temp file and then uses less
on the latter. (That may lose man's own "colorization" unless you play yet further and deeper tricks, etc, etc -- similarly you might use the "preformat pages" option of man
and uncompress such a preformatted page to a tempfile on which to run less
, etc, but this is starting to become a somewhat complex "simple script";-).
Solution
A less manual version of knitatoms' answer
combined with Alex Marteilli's answer
works quite well: pass the +Gg
option to less
via its pager option.
For example, try
man -P 'less -s -M +Gg' man
This can be effected permanently by putting
export MANPAGER='less -s -M +Gg'
in one of your shell configuration files (above syntax is for Bash and
ZSH). Now, for example, man man
displays the percentage as you
wanted!
Warning
You should not put the +Gg
in the LESS
variable! For example,
doing
export LESS='-M +Gg'
will cause problems when reading very large files. For example,
yes | LESS='-M +Gg' less
does not work very well ...
Explanation
As other answers have explained, the problem is that less
can't say
what percent into the file you are until it knows how long the file
is, and it doesn't read to the end of the file by default when reading
from a pipe.
From the OPTIONS
section of man less
:
+ If a command line option begins with +, the remainder of that
option is taken to be an initial command to less. For exam‐
ple, +G tells less to start at the end of the file rather than
the beginning, and +/xyz tells it to start at the first occur‐
rence of "xyz" in the file. As a special case, +<number> acts
like +<number>g; that is, it starts the display at the speci‐
fied line number (however, see the caveat under the "g" com‐
mand above). If the option starts with ++, the initial com‐
mand applies to every file being viewed, not just the first
one. The + command described previously may also be used to
set (or change) an initial command for every file.
The g
means "return to the beginning of file".
The -M
tells less
to show a "long prompt", which in particular includes the percentage. But it seems man
makes less
include the percentage automatically, even if you leave -M
out, if man
detects a "recent version of less
". See the -r prompt
section of man man
for more info.
From the man man
(in 2013):
-P pager, --pager=pager
Specify which output pager to use. By default, man uses pager
-s. This option overrides the $MANPAGER environment variable,
which in turn overrides the $PAGER environment variable. It
is not used in conjunction with -f or -k.
The value may be a simple command name or a command with argu‐
ments, and may use shell quoting (backslashes, single quotes,
or double quotes). It may not use pipes to connect multiple
commands; if you need that, use a wrapper script, which may
take the file to display either as an argument or on standard
input.
Note that it says -s
is the default option used with the pager by man
. In 2022 I no longer see the -s
mentioned here in man man
, but I don't see any harm in leaving it in (it squashes consecutive blank lines).