Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: when matching a string across multiple lines using \_. in regex, the :yank command only works for the first line

Tags:

regex

vim

I want to extract multiple occurences of some text that spans multiple lines and can be matched with a single Vim regex (using meta character \_). Unfortunately, althouth the matching lines are properly highlighted in Vim, when I add any Vim command (like delete or yank) after the matching regexp, the command only works on the first line of each match.

Example:

 1: bad_function(arg1,  
 2:              arg2, arg3,
 3:              ...
 4:              argN);
 5: good_function();
 6: ...
 7: bad_function(arg2_1,
 8:              ...
 9:              arg2_N);
10: another_good_function();  

If I execute :g/bad_function([^;]\+\_[^;]\+;$/d, then only lines 1 and 7 get deleted although highlighted are lines 1-4 and 7-9.

How to yank/delete all the matched (highlighted) lines?

like image 488
tombkeeper Avatar asked Nov 27 '10 06:11

tombkeeper


2 Answers

Try this:

:let @a=''
:g/first\_.*second.*$/normal! v/second^M$"Ay

Enter the ^M with CTRL-V then Enter.

like image 162
Benoit Avatar answered Nov 16 '22 02:11

Benoit


In order to accumulate matching ranges of lines in a register, one can use the following command.

:let @a='' | g/^first/,/^second/y A
like image 39
ib. Avatar answered Nov 16 '22 04:11

ib.