Cursor 会读取并索引你的项目代码库来驱动各项功能。用根目录下的 .cursorignore
文件控制 Cursor 能访问哪些目录和文件。
.cursorignore
中列出的文件会被排除在以下范围之外:
由 Agent 发起的工具调用(比如 terminal 和 MCP servers)无法阻止
访问受 .cursorignore
约束的代码
安全:限制对 API 密钥、凭证和敏感信息的访问。虽然 Cursor 会阻止访问被忽略的文件,但由于 LLM 的不可预测性,无法保证绝对防护。
性能:在大型代码库或 monorepo 中,排除无关部分,以加快索引并提高文件检索的准确性。
在用户设置中为所有项目配置忽略模式,这样就能无需逐个项目设置就排除敏感文件。
默认模式包括:
- 环境文件:
**/.env
、**/.env.*
- 凭据:
**/credentials.json
、**/secrets.json
- 密钥:
**/*.key
、**/*.pem
、**/id_rsa
在项目根目录创建一个遵循 .gitignore
语法的 .cursorignore
文件。
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
*.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 官方关于 gitignore 模式的文档。
用 git check-ignore -v [file]
来测试匹配模式。