VIM: Yank multiple non-continous chunks of text into a registers

Solution 1:

You can use "Myy (capital letter M) to append into register m. This allows you write a global command to yank all lines matching def.*$ and yank all the lines into the m register. Then all you have to do is paste the m register to get the contents "mp

:g/def.*$/normal "Myy

The above global command tell you to find all lines that match the pattern def.*$ and execute the command "Myy in normal mode.

However this has one problem. What happens if the m register is not empty when you start. Well you will end up with what ever you put into the register last time plus all the stuff you appended.

To empty the register you can use to set the register to the empty string.

:let @m=''

So in vim you would type

:let @m=''
:g/def.*$/normal "Myy

To yank everything that matches def.*$ into the m register.