1

I have a python code with several multi-line comments like this:

def hello:
    ''' some comment
        some more text
        some mooore text
    '''

def ciaoo:
    ''' some comment
        some more text
        some mooore text
    '''

These exist throughout the file. I need to get rid of them.

I tried something like: :%s/'''.*'''//g and %s/'''\(\_.*\)'''//g. But turns out I really suck at regex.

Thanks!

3
  • What about %s/\s*\('''\)\_.\{-}\1\n// ?
    – statox
    Feb 19, 2021 at 12:23
  • That worked! Thanks! Do you mind writing this as an answer so that I can mark it answered? (and also possibly explain it, if it's fine, as I still don't understand how it worked while mine didn't :D ) Feb 19, 2021 at 12:48
  • @Ixvs was faster than me but their answer is good :)
    – statox
    Feb 19, 2021 at 14:53

1 Answer 1

3

Explanation for %s/\s*\('''\)\_.\{-}\1\n// in the comment:

  • \s*\('''\) means some or none white spaces and following '''.
  • \_. matches any character, including new lines.
  • \{-} in Vim is equivalent to *? in regex, which means none or some, but as short as possible. E.g. for book, bo* matches boo, but bo*? matches b (none of o was matched), while both bo*k and bo*?k matches book.
    • BTW, in Vim, \{} = *,
  • \1 refers to first parenthesized expression, i.e. ''' in this case.
  • \n means a new line after second '''.
1
  • 1
    Activating the very magic mode can certainly make this easier on the eyes %s/\v\s*(''')\_.{-}\1\n//
    – iruvar
    Feb 19, 2021 at 20:55

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.