I have some lines of text that follow a pattern and some that don't. I'd like to copy the lines that follow this pattern and then copy/yank them into a register so that I can paste them as is someplace else.


def function_1(param1,param2):
    // do something1
    // do something2
    // do something3

def function_Nullify(param=None):
    // does nothing

Now I'd basically like to select the lines that match def.*$, copy them and paste them.

so the output would kindof look like this:

def function_1(param1,param2):
def function_Nullify(param=None):

I want to know if it's possible to do this in vim.

Something like copy matched regex lines into register "m. Then I'd be able to do a "mp where ever else I need this.

I understand that something like sed, awk or grep with some redirection operators might be better suited to this task, but I'd like to know if I can use vim to do this.

  • 2
    If you want to index your code, you might be interested in ctags or cscope.
    – romainl
    Jun 16, 2013 at 5:20
  • I didn't know about these. Thanks! ctags seems like it'll help a lot.
    – ffledgling
    Jun 16, 2013 at 12:39
  • Random downvoter, please explain your downvote alteast?
    – ffledgling
    Nov 29, 2013 at 7:22

1 Answer 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.

  • 5
    A shorter alternative: :g/def.*$/y M.
    – romainl
    Jun 16, 2013 at 5:18
  • How do I yank a particular submatch? Say I want just function names and have the following pattern that puts them into \1: def\ \([^(]*\).*$. How to modify the :g... command to yank \1 rather than the full match?
    – taketwo
    Jun 16, 2013 at 9:36
  • @taketwo the submatch() function seems like it might help you in what you're trying to do. I haven't been able to use it properly yet though.
    – ffledgling
    Jun 16, 2013 at 12:50
  • @romainl I didn't know you could use y after a g... command. Thanks!
    – ffledgling
    Jun 16, 2013 at 12:50
  • @taketwo, unfortunately you can't reuse capture groups from the :g command. What you ask could be done with a combination of :global and a macro, though.
    – romainl
    Jun 16, 2013 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.