I understand that Vim does not support regex capture groups. The problem I am trying to solve is being able to easily capture parts of lines. For example, given the following line of python source code:

def is_string(s, *args):

I would like to do something equivalent to using the following PCRE regex, which captures the function's name:

'^def (\w+) ?\(.*'

(the capture group here would match is_string

What would be the idiomatic way in VimScript to specify a regex pattern for what part of a line I am trying to match?


1 Answer 1


I understand that Vim does not support regex capture groups

I'm not sure where you got that idea from. Vim-regex most definitely supports capture groups. It wouldn't be very useful without them...

Your regex is already pretty much right. Just a couple differences with which characters need to be escaped or not:

^def \(\w\+\) \?(.*

Or if you want to use very magic,

\v^def (\w+) ?\(.*
  • 2
    s/magic/very magic
    – Mass
    Commented Jun 11, 2019 at 21:36
  • 2
    Thanks for the answer!! I've realized I didn't ask the right question. I was specifically trying to extract a capture group in VimScript, assigning the contents of a capture group to a variable. After some more digging I found out about the matchlist() function which does exactly what I want! Appreciate your time! Commented Jun 12, 2019 at 4:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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