|
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 |
2 | 2 |
|
3 | 3 |
|
4 | 4 | VIM REFERENCE MANUAL by Bram Moolenaar
|
@@ -1501,37 +1501,51 @@ Finally, these constructs are unique to Perl:
|
1501 | 1501 | ==============================================================================
|
1502 | 1502 | 11. Fuzzy matching *fuzzy-matching*
|
1503 | 1503 |
|
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. |
1511 | 1506 |
|
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: |
1513 | 1523 | https://github.com/jhawthorn/fzy
|
1514 | 1524 |
|
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. |
1535 | 1549 |
|
1536 | 1550 | The "f" flag of `:vimgrep` enables fuzzy matching.
|
1537 | 1551 |
|
|
0 commit comments