如何使 Less 以百分比形式指示位置
我现在的目标是在您运行命令时也显示百分号,例如,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
解决方案
knitatoms 的答案的较少手动版本
结合 Alex Marteilli 的回答
效果很好:通过其分页器选项将
+Gg
选项传递给less
。例如,尝试
这可以通过放入
shell 配置文件之一来永久生效(以上语法适用于 Bash 和
ZSH)。 例如,现在
man man
显示百分比,因为您通缉!
警告
您不应该将
+Gg
放入LESS
变量中! 例如,这样做
读取非常大的文件时 会导致问题。 例如,
效果不太好...
解释
正如其他答案所解释的那样,问题是
less
不能说在它知道文件有多长之前,您在文件中的百分比是多少
是的,并且读取时默认不会读到文件末尾
从管道中。
来自
man less
的OPTIONS
部分:g
表示“返回文件开头”。-M
告诉less
显示“长提示”,其中特别包括百分比。 但似乎man
会让less
自动包含百分比,即使您将-M
排除在外,如果man
检测到“less
的最新版本”。 有关详细信息,请参阅man man
的-r Prompt
部分。来自
man man
(2013 年):请注意,它说
-s
是man
与寻呼机一起使用的默认选项。 到 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 toless
via its pager option.For example, try
This can be effected permanently by putting
in one of your shell configuration files (above syntax is for Bash and
ZSH). Now, for example,
man man
displays the percentage as youwanted!
Warning
You should not put the
+Gg
in theLESS
variable! For example,doing
will cause problems when reading very large files. For example,
does not work very well ...
Explanation
As other answers have explained, the problem is that
less
can't saywhat 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 ofman less
:The
g
means "return to the beginning of file".The
-M
tellsless
to show a "long prompt", which in particular includes the percentage. But it seemsman
makesless
include the percentage automatically, even if you leave-M
out, ifman
detects a "recent version ofless
". See the-r prompt
section ofman man
for more info.From the
man man
(in 2013):Note that it says
-s
is the default option used with the pager byman
. In 2022 I no longer see the-s
mentioned here inman man
, but I don't see any harm in leaving it in (it squashes consecutive blank lines).更一般地说,
LESS
环境变量可能包含与运行less
时可以显式传递的命令行标志等效的选项 - 这里是-m
告诉它提示更丰富的选项(包括百分比,如您所要求的)。 您还可以在单个环境变量中传递多个选项,方法是每个选项都以$
结尾。 有关详细信息,请参阅 less 的联机帮助页。编辑:less当然有可能不知道< /em> 它将显示的总大小,在这种情况下,它当然不能显示%——在这种情况下,它会提示它所拥有的信息,例如,到目前为止它显示了多少文本。 例如,man 确实通过管道使用了 less 。
因此,如果您的具体需求是查看
man
中的 %(而不是直接在文件上调用less
),您需要使用“备用分页器”(环境变量MANPAGER
或在man
命令行上切换-P
),这是一个保存man
输出的简单脚本到临时文件,然后在后者上使用less
。 (这可能会失去人类自己的“着色”,除非您玩得更深入、更深入的技巧等等——类似地,您可以使用man
的“预格式化页面”选项,并将这样的预格式化页面解压缩为tempfile 用于运行less
等,但这开始变成一个有点复杂的“简单脚本”;-)。More generally, the
LESS
environment variable may contain options equivalent to command line flags you could explicitly pass when runningless
-- 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 useless
that way, by piping.So, if your specific need is to see the % in
man
(rather than when callingless
directly on a file) you need to use an "alternate pager" (environment variableMANPAGER
or switch-P
on theman
command line) which is a simple script that savesman
's output to a temp file and then usesless
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 ofman
and uncompress such a preformatted page to a tempfile on which to runless
, etc, but this is starting to become a somewhat complex "simple script";-).要添加到 Alex Martelli 的答案中:
请注意,您还可以在运行时将任何命令行参数传递给 less,只需键入它(包括 - ),然后按 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
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).
在 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.
对于实时
less
运行时,我使用了-M
。这是
:
提示符处的“-M
Enter”,运行较少。对于非常大的缓冲区,我还必须走到最后才能“发现”它们的大小。 结束并返回首页。
注意:我本来想对 @sleske 的答案发表评论,但最后我记得,评论不不支持
Keyboard
标签。With a live
less
runtime I used-M
.That's "
-M
Enter" 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.我的环境中有这个。 它将打印
; - 底部的 XY 行 Z
,或者至少包含尽可能多的信息。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.