Files
zed/crates/vim/test_data
Sk Akram 1a6de64cf3 vim: Support \+ and \{n\} in substitute patterns (#66515)
## Summary

Closes #66372

In vim mode, `:s` patterns that use `\+` or `\{n\}` matched nothing.
`Replacement::parse` turned vim's `\(` and `\)` into regex groups but
left `\+`, `\{` and `\}` with their backslashes. Zed's regex reads those
as literal `+`, `{` and `}`, so for example `:%s=db\([0-9]\+\)=ch/\1=g`
changed nothing, while Vim and Neovim apply it.

This change also converts `\+`, `\{` and `\}` to `+`, `{` and `}` in the
search part of the pattern, next to the existing parens handling.

Not changed: `:g` has the same parens handling in
`OnMatchingLines::parse` and was left alone, and unescaped `+`, `{` and
`}` behave as before.

## Testing

Added two tests in `crates/vim/src/normal/search.rs`. Both fail without
the change and pass with it:

- `test_replacement_parse_repetition_operators` checks that
`a\([0-9]\+\)b\{2\}` parses to `a([0-9]+)b{2}`.
- `test_replace_repetition_operators` runs `:%s` through vim mode with
`\{2\}`, `\+` and `*` and checks the buffer.

Run on macOS:

- `cargo test -p vim --lib -- repetition_operators`
- `cargo test -p vim --lib` (587 passed)
- `./script/clippy -p vim`
- `cargo fmt -p vim --check`

To try it by hand, enable `vim_mode`, open a file containing `db01`,
`db02`, `db03`, and run `:%s=db\([0-9]\+\)=ch/\1=g`. The lines should
become `ch/01`, `ch/02`, `ch/03`.

Manual check on macOS with a build of this branch:

- `:%s=db\([0-9]\+\)=ch/\1=g` turns `db01`, `db02`, `db03` into `ch/01`,
`ch/02`, `ch/03`. The search bar shows the converted pattern
`db([0-9]+)`.
- For `\{2\}`, the search bar shows the converted pattern
`db([0-9]{2})`, which matches all three lines (3/3).

**Before** (`db01`, `db02`, `db03`):

<img width="2000" height="1259" alt="image"
src="https://github.com/user-attachments/assets/c4306dda-96bc-4c2b-9e2f-912fd65e682f"
/>

**After** `:%s=db\([0-9]\+\)=ch/\1=g`:

<img width="2000" height="1299" alt="image"
src="https://github.com/user-attachments/assets/5a153b7c-1c52-45e6-9d74-caf8ab098fab"
/>

**After** `:%s=db\([0-9]\{2\}\)=ch/\1=g`:

<img width="2000" height="1299" alt="image"
src="https://github.com/user-attachments/assets/e89179c9-ebfa-4dc3-81d1-0bf987a98481"
/>


## Self-review

- [x] I've reviewed my diff for quality, security, reliability, and
performance.
- [x] UI changes follow the
[checklist](https://zed.dev/docs/development/ui-checklist). (No UI
changes.)
- [x] Tests cover the new or changed behavior.

Release Notes:

- Fixed Vim `:s` and `:%s` commands not recognizing `\+` and `\{n\}`
repetition operators in search patterns.

---------

Co-authored-by: dino <dinojoaocosta@gmail.com>
2026-11-04 16:26:39 +00:00
..