Skip to content

Commit c93a2b3

Browse files
girishjichrisbra
authored andcommitted
runtime(doc): Adapt fuzzy doc to reflect 'fzy' algorithm
closes: #17988 Signed-off-by: Girish Palya <[email protected]> Signed-off-by: Christian Brabandt <[email protected]>
1 parent 7e0df5e commit c93a2b3

File tree

1 file changed

+43
-29
lines changed

1 file changed

+43
-29
lines changed

runtime/doc/pattern.txt

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*pattern.txt* For Vim version 9.1. Last change: 2025 Aug 12
1+
*pattern.txt* For Vim version 9.1. Last change: 2025 Aug 13
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -1501,37 +1501,51 @@ Finally, these constructs are unique to Perl:
15011501
==============================================================================
15021502
11. Fuzzy matching *fuzzy-matching*
15031503

1504-
Fuzzy matching refers to matching strings using a non-exact search string.
1505-
Fuzzy matching will match a string, if all the characters in the search string
1506-
are present anywhere in the string in the same order. Case is ignored. In a
1507-
matched string, other characters can be present between two consecutive
1508-
characters in the search string. If the search string has multiple words, then
1509-
each word is matched separately. So the words in the search string can be
1510-
present in any order in a string.
1504+
Fuzzy matching scores how well a string matches a pattern when the pattern
1505+
characters appear in order but not necessarily contiguously.
15111506

1512-
Vim uses the same improved algorithm as the fzy project:
1507+
Example: >
1508+
Pattern: "vim"
1509+
Candidates: "vim" -> perfect
1510+
"vimeo" -> good (v i m)
1511+
"voice mail" -> weaker (v _ i _ _ _ m)
1512+
"vintage" -> no match (no "m")
1513+
<
1514+
If the search string has multiple words, each word is matched separately and
1515+
may appear in any order in the candidate. For example "get pat" matches
1516+
"GetPattern", "PatternGet", "getPattern", "patGetter", "getSomePattern",
1517+
"MatchpatternGet", etc.
1518+
1519+
The 'ignorecase' and 'smartcase' options do not apply, case is ignored if the
1520+
pattern is all lower case.
1521+
1522+
Vim's implementation is based on the algorithm from the fzy project:
15131523
https://github.com/jhawthorn/fzy
15141524

1515-
Fuzzy matching assigns a score for each matched string based on the following
1516-
criteria:
1517-
- The number of sequentially matching characters.
1518-
- The number of characters (distance) between two consecutive matching
1519-
characters.
1520-
- Matches at the beginning of a word
1521-
- Matches at a camel case character (e.g. Case in CamelCase)
1522-
- Matches after a path separator or a hyphen.
1523-
- The number of unmatched characters in a string.
1524-
- A full/exact match is preferred.
1525-
The matching string with the highest score is returned first.
1526-
1527-
For example, when you search for the "get pat" string using fuzzy matching, it
1528-
will match the strings "GetPattern", "PatternGet", "getPattern", "patGetter",
1529-
"getSomePattern", "MatchpatternGet" etc.
1530-
1531-
The functions |matchfuzzy()| and |matchfuzzypos()| can be used to fuzzy search
1532-
a string in a List of strings. The matchfuzzy() function returns a List of
1533-
matching strings. The matchfuzzypos() functions returns the List of matches,
1534-
the matching positions and the fuzzy match scores.
1525+
It uses dynamic programming to compute an optimal score for a given pattern
1526+
and candidate.
1527+
1528+
The algorithm works in two stages:
1529+
1530+
1. Forward pass
1531+
Scan the candidate left to right, tracking the best score for each
1532+
pattern position. Matches score higher when they occur at the start
1533+
of the candidate, the start of a word (space, underscore, dash,
1534+
camelCase), or directly after the previous match.
1535+
1536+
2. Backward pass
1537+
Start from the best-scoring end position and step back to find match
1538+
positions, ensuring the alignment is optimal.
1539+
1540+
Vim extends the original algorithm to support multibyte codepoints, allowing
1541+
correct matching for UTF-8 and other encodings.
1542+
1543+
Time complexity is O(pattern * candidate). Memory usage is proportional
1544+
to the same.
1545+
1546+
The |matchfuzzy()| and |matchfuzzypos()| functions perform fuzzy searching in
1547+
a List of strings. |matchfuzzy()| returns the matching strings, while
1548+
|matchfuzzypos()| returns the matches along with their positions and scores.
15351549

15361550
The "f" flag of `:vimgrep` enables fuzzy matching.
15371551

0 commit comments

Comments
 (0)