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

I want to extract several occurrences of some text that spans multiple lines and can be matched against a single Vim regex (using the \_ metacharacter). Unfortunately, in any case, the corresponding lines are correctly highlighted in Vim, when I add any Vim command (for example, delete or yank) after the corresponding regular expression, 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 are deleted, although lines 1-4 and 7-9 are highlighted.

How to deflate / delete all matched (selected) lines?

+7
vim regex
source share
3 answers

Try the following:

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

Type ^M with CTRL-V and Enter.

+6
source share

As already mentioned in my comment; The related question has a solution to your problem:

 :%g/your_pattern/yank A P 

works for me.

If you want to combine several terms on different lines, you can try (in command mode):

 "zyy (do this on empty line to empty the register 'z') :%g/first.*\|second.*/yank z (grab matches in the 'z' register) P (for pasting the contents of the last register) 
0
source share

To accumulate matching string ranges in a register, you can use the following command.

 :let @a='' | g/^first/,/^second/y A 
0
source share

All Articles