如何将less的当前内容保存到文件中?

Jon*_*Day 112 bash less pipe

如果我已将命令的结果通过管道传送到less,然后决定将内容保存到文件中,这可能吗?

我试过a在缓冲区的末尾设置一个标记,然后返回到顶部并使用|avi将整个内容发送到vi,但这不起作用。

Red*_*ick 123

在我的系统上,man less

s filename
       Save the input to a file.  This only works if  the  input  is  a
       pipe, not an ordinary file.
Run Code Online (Sandbox Code Playgroud)

为我工作!

  • `s` 也不适用于我,但 `-o file` 可以。 (11认同)
  • 不知何故,这对我不起作用 - 键入 's' 将窗口移动一行。我在 Mac 上。 (8认同)
  • @benroth:您可能有一个更改正常命令的 `lesskey` 文件。见`manlesskey` (2认同)

小智 32

接受的答案在 Mac 上不起作用——正如@benroth 所说,按下s只是向下移动——但你可以使用不同的方法。

less --help

|Xcommand            Pipe file between current pos & mark X to shell command.
Run Code Online (Sandbox Code Playgroud)

A mark is any upper-case or lower-case letter.
Certain marks are predefined:
     ^  means  beginning of the file
     $  means  end of the file
Run Code Online (Sandbox Code Playgroud)

因此,如果您转到缓冲区 ( <)的顶部,然后:

|$cat > /tmp/foo.txt
Run Code Online (Sandbox Code Playgroud)

缓冲区的内容将被写出到/tmp/foo.txt.


小智 7

当你less打开,你可以保存完整的输出到文件中。比如vim,less 支持命令。

只需键入 key s,然后less会询问您要保存内容的文件的名称,只需键入文件名,然后键入Enter.

干杯