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
/\%(
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.
-
1Thanks for the explanation! So
\%(
is kind of like non-capturing group to me. Commented May 13, 2020 at 7:49 -
3
-
4Yes,
/\z(
is only valid for syntax highlighting definitions, because you need to definestart
andend
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
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/
-
4Welcome 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)– JoeSchrCommented Jan 29, 2021 at 19:40
-
2Not 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 -
-
On the other hand it was the first thing I found when searching for \zs so maybe keep it up for other lost people– JoeSchrCommented Jan 29, 2021 at 20:05