Cursor针对性配置
一、目标
Cursor 的针对性配置方案目标:
① 针对性修改agent
② 简单 format / 变量名不浪费 token
③ 减少 Cursor 自动吃上下文
④ 修改或增加自定义 API 接入
Cursor 的正确定位应该是:
👉 “受控的代码 diff 工具”,而不是自由发挥的 Copilot
能用 editor / formatter / LSP 解决的,绝不让 AI 插手
二、修改 Cursor 默认 agent行为
1️⃣ 关闭“自动扩展上下文”
Cursor setting
{
"cursor.general.enableDocumentationSearch": false, // 关闭文档搜索
"cursor.chat.streaming": true,
"cursor.chat.defaultModel": "claude-sonnet-4.5", // 用 Sonnet 而非 Opus
}
{
// 限制上下文窗口
"cursor.cpp.disabledLanguages": ["plaintext", "markdown"],
"cursor.chat.contextLength": "short", // short | medium | long
// 禁用不必要的自动索引
"cursor.general.gitIgnore": true, // 遵循 .gitignore
// 限制 @Codebase 范围
"cursor.codebase.indexing.excludePatterns": [
"**/node_modules/**",
"**/dist/**",
"**/build/**",
"**/.next/**",
"**/coverage/**",
"**/*.min.js",
"**/*.bundle.js"
]
}
2️⃣ 固定系统级 Prompt
项目根目录建:
.cursor/instructions.md:
You are an AI coding assistant.
Rules:
- Do NOT reformat code; formatting is handled by Prettier/Black.
- Do NOT rename variables unless explicitly requested.
- Only modify the selected files.
- Output: unified diff only.
侧重行为权限和默认模型策略
.cursorrules:
# Core Principles
- Only modify the specified files/functions; do not rewrite the entire file.
- Simple formatting or renaming tasks should be executed directly without explanation.
- Prefer incremental changes in Git diff style.
- Avoid outputting unrelated code context.
# Response Format
**For simple changes** (formatting, renaming):
- Output only the modified code snippet.
- Do not include confirmation messages like "Understood."
- Do not explain why the change was made.
**For complex changes**:
- Briefly describe the modification in 1-2 sentences.
- Output only the changed code blocks.
- Use `// ...` to indicate unchanged parts.
侧重输出规范、token 优化、简洁 diff
三、把 format / 命名 从 AI 手里“夺权”
1️⃣ Format:100% 交给工具,不给 AI
强烈建议
- Prettier / ESLint / Black / gofmt
- 保存即 format(on save)
Cursor 设置里:
Editor
→ Format on Save = ON
然后在 AI prompt 里明确禁止:
Do NOT format code.
AI 的 format = token 粉碎机 Formatter 的 format = O(1)
2️⃣ 变量命名:只在“新代码”阶段允许 AI
你的工作流应是:
| 场景 | 是否允许 AI 命名 |
|---|---|
| 新函数 / 新模块 | ✅ |
| 既有代码维护 | ❌ |
| refactor | ❌(除非明确) |
Cursor 使用时的固定指令
Keep all existing variable and function names unchanged.
如果你不写这句,Cursor 一定会自作聪明。
四、限制 Cursor 的 token 黑洞行为
1️⃣ 不用“继续修改”“再改一下”
这是 Cursor 最浪费 token 的操作。
正确姿势:
- ❌「再改一下,加个判断」
- ✅ 重新下达完整指令
Cursor 是无状态 diff 工具,不是对话机器人。
2️⃣ 强制 Diff 输出(避免全文回传)
你每次写需求时,都加一句:
Output:
- unified diff only
否则 Cursor 会:
- 重复输出原始代码
- 消耗大量 token
五、修改 / 增加接入 API
1️⃣ Cursor 支持自定义 API Provider
路径:
Cursor Settings
→ Models / Providers
→ Add Custom Provider
你可以接入:
- OpenAI-compatible API
- Azure OpenAI
- 自建 proxy
- 第三方 Claude / Gemini 兼容接口
关键字段(示例)
{
"baseUrl": "https://api.your-endpoint.com/v1",
"apiKey": "sk-xxxx",
"models": [
{
"name": "fast",
"model": "gpt-4o-mini"
},
{
"name": "reasoning",
"model": "o1"
}
]
}
然后在 Cursor 中:
- format / 简单改动 → fast
- 复杂逻辑 → reasoning
2️⃣ 一个高级用法:双模型分工
你可以在 Cursor 里设:
- Default Model:fast / cheap
- 手动切换:reasoning / expensive
原则:
90% 的 Cursor 操作,不值得用高智模型
六、“低 token Cursor 工作流”
1. Formatter 自动处理格式
2. 人判断是否需要 AI
3. Cursor:
- 限文件
- 禁 format
- 禁命名
- 出 diff
4. 复杂判断 → API / Web
当你发现:
- prompt 越来越短
- Cursor 很少“发挥”
- 修改一次成功率 > 80%
说明你已经把 Cursor 驯化成功了。
Cursor 不是“更聪明的编辑器”,而是“被严格约束的 AI 外包程序员”。
单文件临时场景:
1️⃣ 打开单文件
2️⃣ Cursor → Context → Limit to current file
3️⃣ 弹出临时 instructions:
- Only this file
- Do not format
- Keep variable names
- Unified diff
4️⃣ 调用 AI 修改
5️⃣ 用 formatter / lint 做格式化
6️⃣ Review diff → commit
效果:
- AI 只用在增量修改 / 逻辑生成
- token 最小化
- 单文件快速处理
- 不污染全 repo
针对 Cursor Agent 架构,把 Rules、Skills、Subagents、Command 的完整英文配置模板
1️⃣ Rules(行为约束 + 输出规范)
# Rules: Core Principles
- Only modify the selected files/functions; do not rewrite entire files.
- Simple formatting or renaming tasks should be executed directly without explanation.
- Prefer incremental changes in Git diff style.
- Avoid outputting unrelated code context.
# Response Format
**For simple changes** (formatting, renaming):
- Output only the modified code snippet.
- Do not include confirmation messages like "Understood."
- Do not explain why the change was made.
**For complex changes**:
- Briefly describe the modification in 1-2 sentences.
- Output only the changed code blocks.
- Use `// ...` to indicate unchanged parts.
2️⃣ Skills(任务模板 / 能力模块)
{
"skills": [
{
"name": "refactor",
"description": "Perform cross-file refactoring, keeping all unchanged code intact.",
"rules": ".cursorrules.md",
"instructions": [
"Only modify selected functions or files.",
"Output in unified git diff style.",
"Do not change unrelated code."
]
},
{
"name": "format",
"description": "Perform simple formatting or variable renaming.",
"rules": ".cursorrules.md",
"instructions": [
"Apply formatting or renaming directly without explanation.",
"Do not output any confirmation text.",
"Keep changes minimal."
]
},
{
"name": "docgen",
"description": "Generate documentation or comments for specified functions.",
"rules": ".cursorrules.md",
"instructions": [
"Generate concise documentation for selected code only.",
"Do not modify code structure.",
"Use // ... for unchanged parts if including code examples."
]
}
]
}
3️⃣ Subagents(模型/API 指定)
{
"subagents": [
{
"name": "fast_code_agent",
"model": {
"type": "openai",
"url": "https://api.openai.com/v1/chat/completions",
"apiKey": "sk-xxxx",
"modelName": "gpt-4o-mini"
},
"skills": ["format", "refactor"],
"rules": ".cursorrules.md"
},
{
"name": "reasoning_agent",
"model": {
"type": "openai",
"url": "https://api.openai.com/v1/chat/completions",
"apiKey": "sk-xxxx",
"modelName": "gpt-4o",
"temperature": 0
},
"skills": ["refactor", "docgen"],
"rules": ".cursorrules.md"
}
]
}
4️⃣ Command(实际执行指令模板)
{
"commands": [
{
"name": "refactor_selected",
"description": "Refactor selected functions/files with diff output only",
"agent": "fast_code_agent",
"skill": "refactor",
"instructions": "Modify only the selected functions/files. Output unified git diff. Do not change unrelated code."
},
{
"name": "format_code",
"description": "Apply formatting or variable renaming directly",
"agent": "fast_code_agent",
"skill": "format",
"instructions": "Apply formatting or renaming without explanation. Output only the modified code snippet."
},
{
"name": "generate_doc",
"description": "Generate documentation for selected code blocks",
"agent": "reasoning_agent",
"skill": "docgen",
"instructions": "Generate concise documentation for selected code only. Use // ... for unchanged parts."
}
]
}
✅ 特点与说明
- Rules:约束行为和输出,保证 token 高效
- Skills:能力模块化,便于不同任务调用
- Subagents:绑定不同模型/API,区分 fast/ reasoning
- Commands:直接映射到操作指令,便于 GUI 或快捷键执行