## 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>
This contains the code for Zed's Vim emulation mode.
Vim mode in Zed is supposed to primarily "do what you expect": it mostly tries to copy vim exactly, but will use Zed-specific functionality when available to make things smoother. This means Zed will never be 100% vim compatible, but should be 100% vim familiar!
The backlog is maintained in the #vim channel notes.
Testing against Neovim
If you are making a change to make Zed's behavior more closely match vim/nvim, you can create a test using the NeovimBackedTestContext.
For example, the following test checks that Zed and Neovim have the same behavior when running * in visual mode:
#[gpui::test]
async fn test_visual_star_hash(cx: &mut gpui::TestAppContext) {
let mut cx = NeovimBackedTestContext::new(cx).await;
cx.set_shared_state("ˇa.c. abcd a.c. abcd").await;
cx.simulate_shared_keystrokes(["v", "3", "l", "*"]).await;
cx.assert_shared_state("a.c. abcd ˇa.c. abcd").await;
}
To keep CI runs fast, by default the neovim tests use a cached JSON file that records what neovim did (see crates/vim/test_data), but while developing this test you'll need to run it with the neovim flag enabled:
cargo test -p vim --features neovim test_visual_star_hash
This will run your keystrokes against a headless neovim and cache the results in the test_data directory. Note that neovim must be installed and reachable on your $PATH in order to run the feature.
Testing zed-only behavior
Zed does more than vim/neovim in their default modes. The VimTestContext can be used instead. This lets you test integration with the language server and other parts of zed's UI that don't have a NeoVim equivalent.