如何使 Less 以百分比形式指示位置

发布于 2024-07-25 02:50:27 字数 511 浏览 23 评论 0原文

我现在的目标是在您运行命令时也显示百分号,例如,

man emacs

如果您运行命令,您会得到“字节 3300”。

亚历克斯的回答表明我们需要创建一个单独的 shell 函数,其中

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

$1 引用第一个参数。


新问题位于线程感谢 Yuliy 的关键举措!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

筱果果 2024-08-01 02:50:27

解决方案

knitatoms 的答案的较少手动版本
结合 Alex Marteilli 的回答
效果很好:通过其分页器选项将 +Gg 选项传递给 less

例如,尝试

man -P 'less -s -M +Gg' man

这可以通过放入

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

shell 配置文件之一来永久生效(以上语法适用于 Bash 和
ZSH)。 例如,现在 man man 显示百分比,因为您
通缉!

警告

您不应该将+Gg 放入LESS 变量中! 例如,
这样做

export LESS='-M +Gg'

读取非常大的文件时 会导致问题。 例如,

yes | LESS='-M +Gg' less

效果不太好...

解释

正如其他答案所解释的那样,问题是 less 不能说
在它知道文件有多长之前,您在文件中的百分比是多少
是的,并且读取时默认不会读到文件末尾
从管道中。

来自 man lessOPTIONS 部分:

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

g 表示“返回文件开头”。

-M 告诉 less 显示“长提示”,其中特别包括百分比。 但似乎 man 会让 less 自动包含百分比,即使您将 -M 排除在外,如果 man 检测到“less 的最新版本”。 有关详细信息,请参阅 man man-r Prompt 部分。

来自 man man(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.

请注意,它说 -sman 与寻呼机一起使用的默认选项。 到 2022 年,我不再看到 man man 中提到的 -s,但我认为保留它没有任何坏处(它会挤压连续的空白行)。

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

万劫不复 2024-08-01 02:50:27
export LESS="-m"

更一般地说,LESS 环境变量可能包含与运行 less 时可以显式传递的命令行标志等效的选项 - 这里是 -m告诉它提示更丰富的选项(包括百分比,如您所要求的)。 您还可以在单​​个环境变量中传递多个选项,方法是每个选项都以 $ 结尾。 有关详细信息,请参阅 less 的联机帮助页

编辑:less当然有可能不知道< /em> 它将显示的总大小,在这种情况下,它当然不能显示%——在这种情况下,它会提示它所拥有的信息,例如,到目前为止它显示了多少文本。 例如,man 确实通过管道使用了 less 。

因此,如果您的具体需求是查看 man 中的 %(而不是直接在文件上调用 less),您需要使用“备用分页器”(环境变量MANPAGER 或在 man 命令行上切换 -P),这是一个保存 man 输出的简单脚本到临时文件,然后在后者上使用 less 。 (这可能会失去人类自己的“着色”,除非您玩得更深入、更深入的技巧等等——类似地,您可以使用 man 的“预格式化页面”选项,并将这样的预格式化页面解压缩为tempfile 用于运行 less 等,但这开始变成一个有点复杂的“简单脚本”;-)。

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

雨后咖啡店 2024-08-01 02:50:27

要添加到 Alex Martelli 的答案中:

请注意,您还可以在运行时将任何命令行参数传递给 less,只需键入它(包括 - ),然后按 Enter 键。 因此,您只需输入

-m<Enter>

运行 less 即可切换长提示。

这对于需要在运行时更改的选项特别有用,例如-S(行折叠开/关)。

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

剩一世无双 2024-08-01 02:50:27

在 Linux 上,我只需使用 Shift+G 转到手册页的末尾,然后使用 g 返回到开头。 (或者您可以使用 '' 返回到之前的位置)。

Less then 有足够的信息来显示您浏览文件的百分比。 (您可能需要输入 -M 才能获得长提示。)

这有点麻烦,但只需按两次键。 不确定这是否适用于 OS/X。

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.

七月上 2024-08-01 02:50:27

对于实时 less 运行时,我使用了 -M

这是 : 提示符处的“-MEnter”,运行较少。

对于非常大的缓冲区,我还必须走到最后才能“发现”它们的大小。 结束并返回首页


注意:我本来想对 @sleske 的答案发表评论,但最后我记得,评论不不支持 Keyboard 标签。

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.

忘你却要生生世世 2024-08-01 02:50:27

我的环境中有这个。 它将打印 ; - 底部的 XY 行 Z,或者至少包含尽可能多的信息。

export LESS='-P?f%f - .?ltLine?lbs. %lt?lb-%lb.?L of %L.:?pt%pt\%:?btByte %bt:-...'

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:-...'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文