vim: Match Vim's word selection for * and # (#66794)

## Summary

Vim allows searching for punctuation runs using */# and furthermore does
not require the word to be searched for, to be adjacent to the cursor.
See also
[pattern.txt](https://github.com/neovim/neovim/blob/7e394dbb896eaf86cb1046666dd423d512cda55f/runtime/doc/pattern.txt#L62)

Vim's priority for selecting the run to be searched for is:
1. Word under the cursor
2. First word somewhere to the right of the cursor on the same line
3. Punctuation run under the cursor
4. First punctuation run somewhere to the right of the cursor on the
same line

Currently, Zed only allows picking words directly adjacent to the cursor
(both to the left and to the right) and will otherwise not search for
anything.
After this change, Zed will no longer search for a word on the immediate
left of the cursor, which did not match Vim's behavior.
Instead, the chosen run will be selected according to the priorities
outlined above.

## Testing

Pick any file and optionally a Neovim instance and observe that */#
behave as in Neovim.
In particular
* Zed will no longer search for a word on the immediate left of the
cursor.
* Zed will only pick runs that either surround the cursor or are
somewhere to the right of the cursor.
* It is possible to search for punctuation runs.
* Word runs are always prioritized over punctuation runs. 
For example: `(ˇ[ tmp ([tmp` will result in `([ tmp ([ˇtmp` (this is
also included in the test cases).

## Self-review

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

Release Notes:

- Improved `*` and `#` in Vim mode to follow Neovim's behavior and
search for the next word on the line when the cursor isn't on a word,
and for punctuation when no word follows

---------

Co-authored-by: dino <dinojoaocosta@gmail.com>
This commit is contained in:
Moritz Fröhlich
2026-11-18 17:48:16 +00:00
committed by GitHub
parent 988007a89a
commit 846ff80e0e
3 changed files with 202 additions and 8 deletions
+159 -8
View File
@@ -1,6 +1,6 @@
use editor::{Editor, EditorSettings};
use editor::{Editor, EditorSettings, MultiBufferOffset, SelectionEffects};
use gpui::{Action, Context, TaskExt, Window, actions};
use language::Point;
use language::{CharKind, Point};
use schemars::JsonSchema;
use search::{BufferSearchBar, SearchOptions, buffer_search};
use serde::Deserialize;
@@ -454,6 +454,79 @@ impl Vim {
let prior_selections = self.editor_selections(window, cx);
let vim = cx.entity();
let query = self
.update_editor(cx, |_, editor, cx| -> Option<String> {
let display_snapshot = editor.display_snapshot(cx);
let buffer_snapshot = display_snapshot.buffer();
let selection = editor
.selections
.newest::<MultiBufferOffset>(&display_snapshot);
// A selection, such as a visual one, is used as the query
// as is.
// Returning `None` leaves that to the search bar's
// `query_suggestion` further down.
if !selection.is_empty() {
return None;
}
// Like vim, search for the first word at or after the cursor
// on its line, falling back to the first run of punctuation
// when no word follows. The scan only locates the first
// character of that run, `surrounding_word` then expands it
// in both directions.
//
// The offsets stored are the ones right after the character
// rather than before it because, at a boundary,
// `surrounding_word` picks the higher kind of its two
// neighbors, so from the start of `(` in `function_call(` it
// would return `function_call` instead of `(`.
let mut punctuation_offset = None;
let mut word_offset = None;
let classifier = buffer_snapshot.char_classifier_at(selection.start);
for (ch, offset) in display_snapshot
.buffer_chars_at(selection.start)
.take_while(|(ch, _)| *ch != '\n')
{
match classifier.kind(ch) {
// A word always wins over punctuation, so the scan
// can stop at the first one. Punctuation can't stop
// it, since a word may still follow.
CharKind::Word => {
word_offset = Some(offset + ch.len_utf8());
break;
}
CharKind::Punctuation => {
punctuation_offset.get_or_insert(offset + ch.len_utf8());
}
CharKind::Whitespace => {}
}
}
let (range, _) =
buffer_snapshot.surrounding_word(word_offset.or(punctuation_offset)?, None);
// When the run starts to the right of the cursor, vim jumps
// past that occurrence instead of to it.
// As such, we move the cursor onto it first as that makes the
// search treat it as the current match and we avoid scrolling
// as the search will deal with that already.
if move_cursor && range.start > selection.start {
editor.change_selections(
SelectionEffects::no_scroll(),
window,
cx,
|selections| {
selections.select_ranges([range.start..range.start]);
},
);
}
Some(buffer_snapshot.text_for_range(range).collect())
})
.flatten();
let searched = pane.update(cx, |pane, cx| {
self.search.direction = direction;
let Some(search_bar) = pane.toolbar().read(cx).item_of_type::<BufferSearchBar>() else {
@@ -473,11 +546,13 @@ impl Vim {
if !search_bar.show(window, cx) {
return None;
}
let Some(query) = search_bar.query_suggestion(
Some(settings::SeedQuerySetting::Always),
window,
cx,
) else {
let Some(query) = query.or_else(|| {
search_bar.query_suggestion(
Some(settings::SeedQuerySetting::Selection),
window,
cx,
)
}) else {
drop(search_bar.search("", None, false, window, cx));
return None;
};
@@ -784,7 +859,7 @@ mod test {
state::Mode,
test::{NeovimBackedTestContext, VimTestContext},
};
use editor::{DisplayPoint, display_map::DisplayRow};
use editor::{DisplayPoint, HighlightKey, display_map::DisplayRow};
use indoc::indoc;
use itertools::Itertools;
@@ -1107,6 +1182,82 @@ mod test {
cx.set_shared_state("ˇa.c. abcd a.c. abcd").await;
cx.simulate_shared_keystrokes("v 3 l *").await;
cx.shared_state().await.assert_eq("a.c. abcd ˇa.c. abcd");
cx.set_shared_state("foo ˇbar\nbaz\nfoo bar").await;
cx.simulate_shared_keystrokes("shift-v *").await;
cx.shared_state().await.assert_eq("foo bar\nbaz\nˇfoo bar");
}
#[gpui::test]
async fn test_star_hash_under_cursor(cx: &mut gpui::TestAppContext) {
let mut cx = NeovimBackedTestContext::new(cx).await;
// Inside word.
cx.set_shared_state("lˇet x let").await;
cx.simulate_shared_keystrokes("*").await;
cx.shared_state().await.assert_eq("let x ˇlet");
// Word to the right.
cx.set_shared_state("ˇlet x let").await;
cx.simulate_shared_keystrokes("*").await;
cx.shared_state().await.assert_eq("let x ˇlet");
cx.set_shared_state("ˇ let x let").await;
cx.simulate_shared_keystrokes("*").await;
cx.shared_state().await.assert_eq(" let x ˇlet");
// Prioritize word over punctuation.
cx.set_shared_state("(ˇ[ tmp ([tmp").await;
cx.simulate_shared_keystrokes("*").await;
cx.shared_state().await.assert_eq("([ tmp ([ˇtmp");
// Use punctuation if no word to the right.
cx.set_shared_state("tmp(ˇ[ ([").await;
cx.simulate_shared_keystrokes("*").await;
cx.shared_state().await.assert_eq("tmp([ ˇ([");
cx.assert_editor_background_highlights(
HighlightKey::BufferSearchHighlights,
"tmp«([» «([»",
);
cx.set_shared_state("tmpˇ([ ([").await;
cx.simulate_shared_keystrokes("*").await;
cx.shared_state().await.assert_eq("tmp([ ˇ([");
// Assert `#` moves to the previous match.
cx.set_shared_state("tmp([ ˇ([").await;
cx.simulate_shared_keystrokes("#").await;
cx.shared_state().await.assert_eq("tmpˇ([ ([");
cx.assert_editor_background_highlights(
HighlightKey::BufferSearchHighlights,
"tmp«([» «([»",
);
// Count with both `*` and `#`.
cx.set_shared_state("tmp([ ˇ([ ([ ([").await;
cx.simulate_shared_keystrokes("2 *").await;
cx.shared_state().await.assert_eq("tmp([ ([ ([ ˇ([");
cx.simulate_shared_keystrokes("2 #").await;
cx.shared_state().await.assert_eq("tmp([ ˇ([ ([ ([");
cx.assert_editor_background_highlights(
HighlightKey::BufferSearchHighlights,
"tmp«([» «([» «([» «([»",
);
// Both `*` and `#` with `g` modifier.
cx.set_shared_state("ˇhi highlight hi").await;
cx.simulate_shared_keystrokes("*").await;
cx.shared_state().await.assert_eq("hi highlight ˇhi");
cx.simulate_shared_keystrokes("#").await;
cx.shared_state().await.assert_eq("ˇhi highlight hi");
cx.simulate_shared_keystrokes("g *").await;
cx.shared_state().await.assert_eq("hi ˇhighlight hi");
cx.set_shared_state("hi highlight ˇhi").await;
cx.simulate_shared_keystrokes("g #").await;
cx.shared_state().await.assert_eq("hi ˇhighlight hi");
cx.assert_editor_background_highlights(
HighlightKey::BufferSearchHighlights,
"«hi» «hi»ghlight «hi»",
);
}
#[gpui::test]
@@ -0,0 +1,39 @@
{"Put":{"state":"lˇet x let"}}
{"Key":"*"}
{"Get":{"state":"let x ˇlet","mode":"Normal"}}
{"Put":{"state":"ˇlet x let"}}
{"Key":"*"}
{"Get":{"state":"let x ˇlet","mode":"Normal"}}
{"Put":{"state":"ˇ let x let"}}
{"Key":"*"}
{"Get":{"state":" let x ˇlet","mode":"Normal"}}
{"Put":{"state":"(ˇ[ tmp ([tmp"}}
{"Key":"*"}
{"Get":{"state":"([ tmp ([ˇtmp","mode":"Normal"}}
{"Put":{"state":"tmp(ˇ[ (["}}
{"Key":"*"}
{"Get":{"state":"tmp([ ˇ([","mode":"Normal"}}
{"Put":{"state":"tmpˇ([ (["}}
{"Key":"*"}
{"Get":{"state":"tmp([ ˇ([","mode":"Normal"}}
{"Key":"#"}
{"Get":{"state":"tmpˇ([ ([","mode":"Normal"}}
{"Put":{"state":"tmp([ ˇ([ ([ (["}}
{"Key":"2"}
{"Key":"*"}
{"Get":{"state":"tmp([ ([ ([ ˇ([","mode":"Normal"}}
{"Key":"2"}
{"Key":"#"}
{"Get":{"state":"tmp([ ˇ([ ([ ([","mode":"Normal"}}
{"Put":{"state":"ˇhi highlight hi"}}
{"Key":"*"}
{"Get":{"state":"hi highlight ˇhi","mode":"Normal"}}
{"Key":"#"}
{"Get":{"state":"ˇhi highlight hi","mode":"Normal"}}
{"Key":"g"}
{"Key":"*"}
{"Get":{"state":"hi ˇhighlight hi","mode":"Normal"}}
{"Put":{"state":"hi highlight ˇhi"}}
{"Key":"g"}
{"Key":"#"}
{"Get":{"state":"hi ˇhighlight hi","mode":"Normal"}}
@@ -4,3 +4,7 @@
{"Key":"l"}
{"Key":"*"}
{"Get":{"state":"a.c. abcd ˇa.c. abcd","mode":"Normal"}}
{"Put":{"state":"foo ˇbar\nbaz\nfoo bar"}}
{"Key":"shift-v"}
{"Key":"*"}
{"Get":{"state":"foo bar\nbaz\nˇfoo bar","mode":"Normal"}}