在 vi/vim 中,如何附加到文件而不是覆盖它?

Bru*_*aco 15 vim vi text-processing

我知道我可以通过简单地执行:w <file>. 我想知道如何通过附加而不是覆盖来写入文件。

用例示例:我想从日志文件中提取一些样本到另一个文件中。为了实现这一目标,我今天可以做到:

  1. 打开日志文件
  2. 选择一些行 Shift+v
  3. 写入文件: :w /tmp/samples
  4. 选择更多的行 Shift+v
  5. 附加到/tmp/samples:w !cat - >> /foo/samples

不幸的是,第 5 步很长、丑陋且容易出错(缺少 a>会使您丢失数据)。我希望 Vim 在这里有更好的东西。

mur*_*uru 30

来自:h :w

                                                :w_a :write_a E494
:[range]w[rite][!] [++opt] >>
                        Append the specified lines to the current file.

:[range]w[rite][!] [++opt] >> {file}
                        Append the specified lines to {file}.  '!' forces the
                        write even if file does not exist.
Run Code Online (Sandbox Code Playgroud)

因此,如果您使用视觉模式选择了文本,只需执行:w >> /foo/samples(:'<,'>将自动添加)。如果你错过了>,Vim 会抱怨:

E494: Use w or w>>
Run Code Online (Sandbox Code Playgroud)