Visual Studio Code、Chrome开发者工具、Sublime的文件搜索(Ctrl+p/Cmd+p)用何种算法?
Great question! I’ve spent time digging into how Sublime’s legendary fuzzy search works (since it’s the blueprint for similar tools in VS Code and Chrome DevTools), so let’s break this down clearly:
This type of search falls into the category of fuzzy multi-keyword subsequence matching, and the implementation used by these tools combines a handful of key techniques to balance speed and flexibility:
First, Tokenize Everything
Before building an index, filenames are split into logical "tokens" using delimiters like hyphens, underscores, spaces, and even case transitions (e.g., "MyBigFile.js" gets split into ["my", "big", "file", "js"]). For your example filename, that means tokens likebig,beautifully,created,file,something,else.Inverted Index for Fast Candidate Filtering
An inverted index is created where each token maps to the list of files that contain it. This lets the tool quickly narrow down files that include all the query terms you input—regardless of their order in the filename (which is why both "created file" and "file created" hit your target).Subsequence Matching for Partial Matches
For each query term (even partial ones), the tool checks if it’s a subsequence of any token in the filename. A subsequence means the characters appear in order, but don’t have to be consecutive. So typingcre filwould still matchcreatedandfile, becausec-r-eis a subsequence ofcreated, andf-i-lis a subsequence offile. This check is usually done with a lightweight greedy algorithm to keep things snappy.Ranking to Surface the Best Results
Not all matches are created equal. The tool ranks results using factors like:- How early the matched tokens appear in the filename
- How consecutive the matched characters are (exact word matches get a big boost)
- How closely the query terms align with the filename’s tokens
Bonus: n-gram Indexing for Cross-Token Fragments
Some implementations (like VS Code’s) also use n-gram indexing—splitting tokens into small character chunks (e.g., 3-character sequences) and indexing those. This lets the tool quickly catch cross-token fragments likesomething-beautifully(it matches n-grams from bothsomethingandbeautifullyin the filename).
You asked about Tries, which are great for prefix matching (like autocompleting when you type the start of a filename). But they’re a poor fit here: Tries struggle with unordered keywords, partial mid-token matches, and cross-token fragments. The combination of tokenization, inverted indexing, and subsequence matching solves all these gaps far better.
内容的提问来源于stack exchange,提问作者Uthman

