5

I know \(...\) is Vim's way to create a capturing group. It was listed under the atom entry in Vim's doc. There are two other forms of atom, \%(...\) and \z(...\). The document is not very clear on what exactly does they do. Could anyone help to explain it please?

2 Answers 2

6

/\%( permits to define a group as with /\(, but we cannot refer to text matched by this group with /\1 or with submatch() from commands like :substitute or functions like matchlist().

Having a group permits to apply counting and other things to the group (/star, /\+, /\?...)

While /\%( hides, /\z( seems to do the opposite, it exports a group found outside the context where it has been found. It seems to apply only to syntax highlighting. I've never used it.

3
  • 1
    Thanks for the explanation! So \%( is kind of like non-capturing group to me. Commented May 13, 2020 at 7:49
  • 3
    This is exactly a non capturing group. Commented May 13, 2020 at 7:50
  • 4
    Yes, /\z( is only valid for syntax highlighting definitions, because you need to define start and end patterns and you might want to make sure, that the part matched in a start pattern is also in an end pattern available Commented May 13, 2020 at 7:56
2

you can also use it like this

  :%s/file\zsName/Path/ge

which would substitute fileName with filePath without you having to retype file or any other more bothersome to type word just used to identify the search term

use :help /\zs for more info. just learned about it here: http://vimcasts.org/episodes/project-wide-find-and-replace/

8
  • 4
    Welcome to Vi and Vim! This is useful information, but I'm not sure it applies directly to the question (\z vs. \zs).
    – D. Ben Knoble
    Commented Jan 29, 2021 at 19:34
  • Afaik it's the same, but instead of creating a group with (...) you use it as an argument for the substitution expression (the s in \zs)
    – JoeSchr
    Commented Jan 29, 2021 at 19:40
  • 2
    Not quite. \zs (and cousin ze) make the whole match pretend to start (end) in different places. They have more in common with positive lookbehinds (aheads).
    – D. Ben Knoble
    Commented Jan 29, 2021 at 19:51
  • Ah, I understand. You may delete this then.
    – JoeSchr
    Commented Jan 29, 2021 at 20:04
  • On the other hand it was the first thing I found when searching for \zs so maybe keep it up for other lost people
    – JoeSchr
    Commented Jan 29, 2021 at 20:05

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.