23

I now aim to show the percentage sign also when you run, for example, the command

man emacs

If you run it, you get 'byte 3300' for instance.

Alex's answer suggests me that we need to make a separate shell function by

man "$1"| col -b > /tmp/manual
less /tmp/manual

where $1 refers to the first parameter.


The new problem is at the thread. Thanks to Yuliy for the crux move!

seven-phases-max
  • 11,765
  • 1
  • 45
  • 57
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697

6 Answers6

38

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).

ntc2
  • 11,203
  • 7
  • 53
  • 70
  • I am thinking how to extend your code `yes | LESS='-M +Gg' less` here http://stackoverflow.com/q/31027735/54964 I replaced the `yes` function with big data file. Should the situation be the same? – Léo Léopold Hertz 준영 Jun 24 '15 at 13:23
  • Could you also explain the `-s -M` options? It seems to work without them. – tocic Sep 17 '22 at 06:07
  • 1
    @tocic: I updated the answer to explain `-s -M`. It seems they may be irrelevant with modern versions of `man`, but at least the `-s` was relevant when I wrote this answer in 2013. The `-M` is similar to the `-m` mentioned in Alex Martelli's [answer](https://stackoverflow.com/a/1049398/470844). – ntc2 Sep 23 '22 at 06:09
15
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";-).

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • @Alex: It seems that something is wrong in my Less, since I see: http://files.getdropbox.com/u/175564/bugLess.png. When I go to the end of the manual the percentage sign starts to work. --- I disabled my .zshrc so the bug cannot be in it. – Léo Léopold Hertz 준영 Jun 26 '09 at 14:51
  • 1
    It's 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 %. Here, let me edit the answer with this remark for completeness. – Alex Martelli Jun 26 '09 at 15:49
  • @Alex: Please, see my attempt to your suggestions in my question. – Léo Léopold Hertz 준영 Jun 26 '09 at 18:08
  • Your script is correct so I don't understand (2). Nor do I understand what this has to do with emacs or vim so the other points are also mysteries to me, particularly in the context of a question about "making less indicate location in percentage". Why not open several separate questions about (1), emacs stuff, vim stuff, and whatever else? I can try to help with (1) if I see it in a separate question (if you consider my answer to this one question good enough to accept, otherwise I'll know you're just too hard to please;-) but surely won't tackle the emacs one, for example;-). – Alex Martelli Jun 27 '09 at 00:50
  • You are right: the last questions are not relevant anymore. -- I fixed my post. – Léo Léopold Hertz 준영 Jun 27 '09 at 21:24
  • @AlexMartelli: please see [my answer below](http://stackoverflow.com/a/19871578/470844) for a simple way to use the `MANPAGER` variable here. – ntc2 Nov 09 '13 at 02:13
  • 2
    For an even more verbose `less` prompt including file name and line numbers, use `export LESS="-M"`. I added it to my `~/.bashrc`. – Tor Klingberg Jul 22 '15 at 10:04
12

To add to Alex Martelli' answer:

Note that you can also pass any command line parameter to less at runtime, by just typing it (including the -), followed by enter key. So you can just type

-m<Enter>

into a running less to toggle the long prompt.

This is especially useful for options that need to be changed at runtime, e.g. -S (line folding on/off).

sleske
  • 81,358
  • 34
  • 189
  • 227
  • 1
    @sleske: Should the option -S give folds similar as in Vim? --- It seems to remove duplicate empty lines by an empty line. – Léo Léopold Hertz 준영 Jun 26 '09 at 15:01
  • 2
    No, -S (Shift-S) just wraps lines, instead of cutting them off at the edge of the screen. What you were seeing was -s (just small s), which indeed removes duplicate empty lines. – sleske Jun 26 '09 at 21:28
12

On Linux I just go to the end of the man page with Shift+G then return to the beginning with g. (Or you can return to your previous position with '').

Less then has enough information to display the percentage of how far through the file you are. (You might need to type -M to get the long prompt.)

It's a bit of a hack but only two key presses. Not sure if this works on OS/X.

Flimm
  • 136,138
  • 45
  • 251
  • 267
keybits
  • 4,383
  • 2
  • 20
  • 18
3

With a live less runtime I used -M.

That's "-MEnter" at the : prompt with less running.

With really large buffers I also had to go to the end to "discover" their size. End and back Home.


Note: I would have posted as a comment to @sleske's answer, but last I recall, comments don't support <kbd>Keyboard<kbd> tags.

Community
  • 1
  • 1
ThorSummoner
  • 16,657
  • 15
  • 135
  • 147
1

I have this in my environment. It'll print <filename> - Lines X-Y of Z at the bottom, or at least as much of that information as it has.

export LESS='-P?f%f - .?ltLine?lbs. %lt?lb-%lb.?L of %L.:?pt%pt\%:?btByte %bt:-...'
John Kugelman
  • 349,597
  • 67
  • 533
  • 578