You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 like big, 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 typing cre fil would still match created and file, because c-r-e is a subsequence of created, and f-i-l is a subsequence of file. 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 like something-beautifully (it matches n-grams from both something and beautifully in the filename).

Why Not a Trie (or Multi-Way Trie)?

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:11:54