Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy all lines starting with a number in vim?

Tags:

vim

macvim

I have a document containing titles and codes. Each got their own line and now I need to copy all codes out of it, each in a separate line. They all start with numbers and Vim can probably do it with ease.

The document looks like this:

TITLE
123-456-4252-2

Other TITLE 2
123-456-4252-X

A nice TITLE 3
523-456-4252-2

...
like image 837
Woww Avatar asked Dec 31 '22 18:12

Woww


1 Answers

You can use the :global command!

qaq:g/^\d/y A

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.
like image 132
L3viathan Avatar answered Jan 13 '23 15:01

L3viathan