Skip to content

Vim \zs and \ze #26

Open
Open
@OnlineCop

Description

@OnlineCop

I apologize in advance; I am a very infrequent vim user, so some of this may need to be double-checked.

  • \zs sets the start of a match.
  • \ze sets the end of a match.

Vim's \zs appears to work like PCRE's \K (which discards matched text up to that point):

  • PCRE pattern: s/foo\Kfoo/bar/g
  • Vim pattern: :%s/foo\zsfoo/bar/g

Text: foofoofoofoo
Result: foobarfoobar

On the other hand, vim's \ze appears to work more like a lookahead:

  • PCRE pattern: s/foo(?=foo)/bar/g
  • Vim pattern: :%s/foo\zefoo/bar/g
  • Vim's lookahead pattern: :%s/foo\(foo\)\@=/bar/g

Text: foofoofoofoo
Result: barbarbarfoo

I thought it might be useful to include the \K similarities in your Vim cheatsheet.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions