概述
Cursor 会读取并索引您的项目代码库,以支持其功能。您可以在项目根目录创建一个 .cursorignore 文件,控制 Cursor 可以访问的目录和文件。
在 .cursorignore 中列出的文件将被 Cursor 阻止访问,包括:
- 代码库索引
- Tab、Agent 和 Inline Edit 可访问的代码
- 通过
@符号引用可访问的代码
重要提示:由 Agent 发起的工具调用(如终端和 MCP 服务器)无法阻止对受 .cursorignore 控制的代码的访问。
为什么要忽略文件?
- 安全性:限制对 API 密钥、凭据和机密的访问。尽管 Cursor 会阻止被忽略的文件,但由于 LLM 的不可预测性,无法保证完全的保护。
- 性能:在大型代码库或 monorepo 中,排除不相关的部分,以加快索引速度并提高文件发现的准确性。
全局忽略文件
您可以在用户设置中为所有项目定义忽略模式,以便在无需为每个项目单独配置的情况下自动排除敏感文件。
默认模式包括:
- 环境文件:
**/.env、**/.env.* - 凭据:
**/credentials.json、**/secrets.json - 密钥:
**/*.key、**/*.pem、**/id_rsa
配置 .cursorignore
在项目根目录创建一个 .cursorignore 文件,使用与 .gitignore 相同的语法。
示例模式
config.json # 特定文件
dist/ # 目录
*.log # 文件扩展名
**/logs # 嵌套目录
!app/ # 取消忽略(否定)
分层忽略
启用 Cursor Settings > Features > Editor > Hierarchical Cursor Ignore,以在父目录中搜索 .cursorignore 文件。
注意:注释以 # 开头。后面的模式会覆盖前面的模式。模式是相对于文件位置的。
使用 .cursorindexingignore 限制索引
使用 .cursorindexingignore 仅将文件排除在索引之外。这些文件仍可被 AI 功能访问,但不会出现在代码库搜索中。
默认忽略的文件
Cursor 会自动忽略 .gitignore 中的文件,以及以下默认忽略列表中的文件。您可以在 .cursorignore 中使用前缀 ! 来覆盖此设置。
默认忽略列表
仅用于索引,除了您的 .gitignore、.cursorignore 和 .cursorindexingignore 中的文件外,还会忽略以下文件:
package-lock.json
pnpm-lock.yaml
yarn.lock
composer.lock
Gemfile.lock
bun.lockb
.env*
.git/
.svn/
.hg/
*.lock
*.bak
*.tmp
*.bin
*.exe
*.dll
*.so
*.lockb
*.qwoff
*.isl
*.csv
*.pdf
*.doc
*.xls
*.xlsx
*.ppt
*.pptx
*.odt
*.ods
*.odp
*.odg
*.odf
*.sxw
*.sxc
*.sxi
*.sxd
*.sdc
*.jpg
*.jpeg
*.png
*.gif
*.bmp
*.tif
*.mp3
*.wav
*.wma
*.ogg
*.flac
*.aac
*.mp4
*.mov
*.wmv
*.flv
*.avi
*.zip
*.tar
*.gz
*.7z
*.rar
*.tgz
*.dmg
*.iso
*.cue
*.mdf
*.mds
*.vcd
*.toast
*.img
*.apk
*.msi
*.cab
*.tar.gz
*.tar.xz
*.tar.bz2
*.tar.lzma
*.tar.Z
*.tar.sz
*.lzma
*.ttf
*.otf
*.pak
*.woff
*.woff2
*.eot
*.webp
*.vsix
*.rmeta
*.rlib
*.parquet
*.svg
.egg-info/
.venv/
node_modules/
__pycache__/
.next/
.nuxt/
.cache/
.sass-cache/
.gradle/
.DS_Store/
.ipynb_checkpoints/
.pytest_cache/
.mypy_cache/
.tox/
.git/
.hg/
.svn/
.bzr/
.lock-wscript/
.Python/
.jupyter/
.history/
.yarn/
.yarn-cache/
.eslintcache/
.parcel-cache/
.cache-loader/
.nyc_output/
.node_repl_history/
.pnp.js/
.pnp/
否定模式的限制
使用否定模式(以 ! 为前缀)时,如果父目录通过 * 被排除,则无法重新包含文件。
示例
# 忽略 public 文件夹中的所有文件
public/*
# ✅ 这有效,因为文件位于顶层
!public/index.html
# ❌ 这无效——无法重新包含嵌套目录中的文件
!public/assets/style.css
解决方法
明确排除嵌套目录:
public/assets/*
!public/assets/style.css # 这个文件现在可访问
说明:出于性能原因,排除的目录不会被遍历,因此针对其中文件的模式不会生效。这与 .gitignore 对嵌套目录中否定模式的实现一致。
故障排除
使用 git check-ignore -v [file] 测试模式。
忽略文件语法完全遵循 .gitignore。如果遇到问题,可以在搜索查询中将 "cursorignore" 替换为 "gitignore",在 Stack Overflow 等平台查找类似问题的解决方案。