138

I notice the standard regex syntax for matching across multiple lines is to use /s, like so:

This is\nsome text
/This.*text/s

This works in Perl for instance but doesn't seem to be supported in Vim. Instead, I have to be much more specific:

/This[^\r\n]*[\r\n]*text/

I can't find any reason for why this should be, so I'm thinking I probably just missed the relevant bits in the vim help.

Can anyone confirm this behaviour one way or the other?

1 Answer 1

217

Yes, Perl's //s modifier isn't available on Vim regexes. See :h perl-patterns for details and a list of other differences between Vim and Perl regexes.

Instead you can use \_., which means "match any single character including newline". It's a bit shorter than what you have. See :h /\_..

/This\_.*text/
5
  • 5
    Nice - my life just got a little easier. I'd been doing (.|\n)*
    – ojrac
    May 25, 2010 at 1:23
  • 3
    What does the forward slash in the :h /\_. do? I mean, I know it works, but why does :h /\_. work, and not :h \_. ?
    – Eddified
    Jul 12, 2012 at 23:49
  • 3
    @Eddified the forward slash (which is the default key for searching) specifies the context of \_. which you can read like: give me help for the search expression \_.
    – Florian
    Mar 20, 2016 at 15:40
  • 1
    However, it seems that I can't perform lazy matching with this expression, even if I included it in parentheses? Is there any way to do it?
    – xji
    Sep 8, 2016 at 18:07
  • 10
    @JIXiang For lazy match on foo bar \n foo baz \n foo, try /foo\_.\{-}foo Mar 7, 2017 at 17:52

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.