你如何运行 `less` 并让它自动运行搜索模式?

Nei*_*eil 14 linux unix less

我正在尝试less在 Linux 中运行,我希望它在启动后立即搜索某些内容。

基本上就像这样做:

$ less
Run Code Online (Sandbox Code Playgroud)

然后按'/',输入类似 的搜索模式"^commit \w+$",然后按 Enter,然后按'n'查找每个后续结果。

我希望少推出,然后搜索模式。手册页中似乎没有任何关于从模式开始的内容,但也许您可以像 Vim 一样向它发送命令。

Mat*_*hen 16

您可以使用 + 发送任意命令。例如:

less +/pattern
Run Code Online (Sandbox Code Playgroud)

-p PATTERN(由 nik 发布)相当于+/PATTERN.

  • 我更喜欢这个答案,因为它更完整。选项`+/pattern` 等价于`--pattern=pattern`,但它可以做更多的事情,比如在文件末尾少开始,做`less +G`。`+` 选项为您提供了 `-p|--pattern` 所做的一切,以及可以在 less 中运行的整套其他命令,所以我说不要费心记住 `-p`,只需使用 `+/` 来搜索就像你会从里面少。 (2认同)

gol*_*udo 10

man页面:

   -ppattern or --pattern=pattern
          The  -p  option  on the command line is equivalent to specifying
          +/pattern; that is, it tells less to start at the  first  occur-
          rence of pattern in the file.
Run Code Online (Sandbox Code Playgroud)

使用最新版本的less( 436 )可以按预期工作。