Move lines matched by :g to the top of the file

Viewed 1815

I have a large text file with several calls to a specific function method_name.

I've matched them using :g/method_name.

How would I move them to the top of the file (with the first match being on the top)?

I tried :g/method_name/normal ddggP but that reverses the order. Is there a better way to directly cut and paste all the matching lines, in order?

Example input file:

method_name 1
foo
method_name 2
bar
method_name 3
baz

Example output file:

method_name 1
method_name 2
method_name 3
foo
bar
baz
3 Answers
Related