Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VIM.. Yank multiple rows based on regex

Tags:

vim

Here's my situation I have numerous SQL rows like the following.

SET IDENTITY_INSERT blah
DELETE FROM blah
SET IDENTITY_INSERT blah
DELETE FROM blah
SET IDENTITY_INSERT blah
DELETE FROM blah
SET IDENTITY_INSERT blah
DELETE FROM blah
SET IDENTITY_INSERT blah
DELETE FROM blah

I want to rearrange it using VIM to

SET IDENTITY_INSERT blah
SET IDENTITY_INSERT blah
SET IDENTITY_INSERT blah
SET IDENTITY_INSERT blah
DELETE FROM blah
DELETE FROM blah
DELETE FROM blah

now I know I could write a simple python script to accomplish this quickly but I'm trying to up my VIM skills.

like image 414
Rob Avatar asked Dec 26 '10 16:12

Rob


2 Answers

You can use g command:

:g/^DELETE/m$

This will move (m) all lines starting with DELETE to the end of the file ($).

like image 101
zeuxcg Avatar answered Sep 18 '22 10:09

zeuxcg


the following does not use any regex...

:sort!

details : http://vim.wikia.com/wiki/Sort_lines

like image 31
ajreal Avatar answered Sep 22 '22 10:09

ajreal