如何在vim中复制以数字开头的所有行?

Wow*_*oww 4 vim macvim

我有一个包含标题和代码的文档。每个代码都有自己的行,现在我需要复制其中的所有代码,每个代码都放在单独的行中。它们都以数字开头,Vim可以轻松实现。

该文档如下所示:

TITLE
123-456-4252-2

Other TITLE 2
123-456-4252-X

A nice TITLE 3
523-456-4252-2

...
Run Code Online (Sandbox Code Playgroud)

L3v*_*han 9

You can use the :global command!

qaq:g/^\d/y A
Run Code Online (Sandbox Code Playgroud)

After this, the lines are in the a register. Afterwards you can paste the copied lines with "ap.

Explanation:

  • qaq records a macro in a and immediately ends it, effectively clearing it.
  • :g/foo/bar executes the bar command for every line that matches foo.
  • ^\d is a regex that matches lines starting with a number.
  • y A yanks into the a register, but in append-mode.

  • 非常感谢,vim功能强大,但很难学习。谢谢您帮助像我这样的菜鸟:) (2认同)
  • 稍作更改,就可以将内容添加到剪贴板:`qaq:g / ^ \ d / y A | 让@ + = @ a` (2认同)