Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move all the texts with a given pattern to the top of the file in Vim?

Tags:

vim

I know

g/PATTERN/m $

will move all the texts with matching PATTERN to the end of the file. How do I accomplish the opposite? (i.e. to the top of the file)?

like image 495
Alby Avatar asked Jan 27 '26 17:01

Alby


1 Answers

Change $ to 0

:g/PATTERN/m0

If you want it in the same order as they are in the file run the command twice.

Or all at once. execute is needed since g can not be chained with bar. The second g command will use the same pattern as the first.

:exec 'g/PATTERN/m0' | g//m0

the command after the global command is :move which moves the current line to whatever address is supplied to move. 0 represents the first line in the file and $ represents the last.

like image 119
FDinoff Avatar answered Jan 30 '26 05:01

FDinoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!