面向已掌握 Cursor 基础操作的进阶用户。本文聚焦快捷键工作流、多光标+AI、代码审查重构调试、Git 集成和 Chat History 管理。
一、快捷键工作流
1.1 核心 AI 快捷键矩阵
| 操作 | macOS | Windows/Linux | 说明 |
|---|---|---|---|
| 打开 Chat | ⌘L | Ctrl+L | 对话式 AI 助手 |
| 行内编辑 | ⌘K | Ctrl+K | 选中代码后告诉 AI 如何修改 |
| Composer | ⌘I | Ctrl+I | 从零生成或大幅重构 |
| 快速修复 | ⌘. | Ctrl+. | AI 驱动的 Quick Fix |
| 接受 diff | Tab | Tab | 接受 AI 建议 |
| 拒绝 diff | Esc | Esc | 放弃 AI 建议 |
| 逐块审查 | ⌘Shift+Y/N | Ctrl+Shift+Y/N | 逐段审查 AI 修改 |
1.2 黄金工作流:⌘K → ⌘L 交替使用
- 选中需要重构的代码
- ⌘K → 输入修改指令
- AI 生成 diff → Tab 接受/ Esc 拒绝
- 不满意但方向正确 → ⌘L 打开 Chat 追问细节
- Chat 中获取新方案 → 复制粘贴
1.3 自定义快捷键绑定
推荐添加到 keybindings.json:
[
{ "key": "ctrl+cmd+space", "command": "cursor.ai.quickChat", "when": "editorTextFocus" },
{ "key": "shift+cmd+r", "command": "cursor.ai.refactor", "when": "editorHasSelection" },
{ "key": "alt+cmd+l", "command": "cursor.explainCode", "when": "editorHasSelection" }
]二、多光标与 AI 结合
2.1 AI 增强的多光标操作
场景:批量将 console.log 替换为结构化日志
- ⌘D 逐个选中所有 console.log
- ⌘Shift+L 选中所有匹配项
- ⌘K → "Convert each to logger.info with the file/module name as context"
场景:重复代码结构统一
多个相似函数需要统一参数风格时,Alt+Shift+拖拽选择列区域 → ⌘K → "Convert all to destructured options object pattern"
2.2 跨文件批量修改
- ⌘Shift+F 搜索模式
- 搜索结果中 ⌘A 全选
- ⌘Shift+L 将每个搜索结果位置设为光标
- ⌘K → 输入修改指令,AI 在每个位置独立应用
2.3 多光标代码审查
- 用 ⌘D 选中某模式的所有出现
- ⌘L → "Review all these usages. Are there any that lack null checks?"
- AI 在 Chat 中逐个分析,指出有问题的位置
三、代码审查与重构调试
3.1 AI 驱动的 Code Review
审查整个文件的提示词模板:
Review this file for:
- Potential bugs or edge cases
- Performance bottlenecks
- Security vulnerabilities (XSS, injection, etc.)
- Code style violations
- Missing error handling
- Suggest specific fixes with code snippets审查 React 组件:
Review this React component. Check for:
1. Missing useCallback/useMemo optimizations
2. Potential infinite re-render loops
3. Accessibility issues (ARIA attributes, keyboard nav)
4. State management anti-patterns3.2 AI 重构策略
渐进式重构(推荐,安全):
- 选中要重构的代码块
- ⌘K → "Extract this block into a custom hook named useUserAuthentication"
- 用 ⌘Shift+Enter 预览完整 diff 后再接受
- 如果涉及多文件,⌘L 追问 import 语句
大规模重构(Composer):
Cmd+I 打开 Composer:
Refactor this Express.js API to use async/await.
Rules:
- Keep existing error handling middleware
- Use try/catch blocks
- Do NOT change the API contract3.3 AI 调试工作流
异常调试: 复制错误堆栈 → ⌘L 粘贴 → "Analyze this stack trace and identify the root cause"
主动调试:
I have a race condition in this async queue processor... Help me:
1. Identify the exact race window
2. Suggest a fix using either a lock or atomic operations
3. Show me the modified code高阶调试技巧:
- Rubber duck 模式:粘贴代码问 "Walk through this code line by line"
- 断点建议:粘贴代码问 "Where should I place breakpoints to diagnose a memory leak?"
- 日志注入:选中可疑代码段 → ⌘K → "Add debug console.log statements"
四、Git 深度集成
4.1 AI 生成提交信息
在 Source Control 面板(⌘Shift+G)暂存文件后,点击提交信息框上方的 AI 生成按钮,或使用命令面板中的 "Generate Commit Message"。
AI 分析 diff 生成 Conventional Commits 格式的消息。
自定义提交风格可以在 .cursorrules 中配置:
When generating commit messages:
- Always follow Conventional Commits format
- Include scope (component/module name)
- Maximum 72 characters per line
- Use imperative mood4.2 AI 解决合并冲突
- 打开冲突文件 → ⌘L
- 提示:
I have merge conflicts in this file. For each conflict:
- Explain what both sides are trying to do
- Recommend which version to keep, or how to combine them
- Show the conflict-resolved code对于大型合并(20+ 冲突文件),让 AI 扫描所有冲突文件并按类型分类,自动解决琐碎冲突。
4.3 Git Blame 增强
Show me the git blame history for this function. For each author:
- What did they change?
- Was it part of a feature or a bug fix?
- Are there any regression patterns?4.4 代码审查评论辅助
在 GitHub PR review 中使用 Cursor:打开 PR diff → 选中有疑问的代码段 → ⌘K → "Generate a constructive code review comment explaining why this approach might cause issues and suggest an alternative"
五、Chat History 高级用法
5.1 会话管理策略
策略一:按功能模块分会话
- Chat 1: "用户认证模块"
- Chat 2: "支付集成"
- Chat 3: "数据库优化"
策略二:关键修改后 Reset
完成一个重要重构后,在 Chat 面板中 "Clear Context",开始新的对话,避免旧的上下文污染新的查询。
策略三:问题-解决结对
同一个 Bug:
- 第一次 Chat: "什么原因导致崩溃" → AI 分析
- 应用修复后 → 新建 Chat
- 第二次 Chat: "验证修复是否正确,检查是否有类似问题"
5.2 发掘历史会话的价值
回滚到某个对话断点: 打开 Chat History 列表 → 找到修复前的对话 → 右键 "Restore Chat"
从历史中提取模式:
Based on our previous conversations, what recurring issues have we encountered with the state management? Create a checklist of things to verify when adding new state.5.3 手动注入上下文
当 Chat History 因为 Clear Context 而丢失有用信息时:
(CONTEXT RESTORATION) Here is the context from our previous session:
- We're building a microservices architecture
- Using gRPC for inter-service communication
- We decided to use Saga pattern for distributed transactions
Now, with this context restored, let me ask about adding a new service...5.4 Chat Export 文档化
定期导出重要的 Chat History 到 docs/ai-decisions/ 目录:
# docs/ai-decisions/2024-03-auth-refactor.md
## Context
Discussed refactoring JWT auth to use refresh tokens...
## Key Decisions
1. Access token: 15min expiry, stored in memory
2. Refresh token: 7 days, httpOnly cookie这样即使 Chat History 清除了,记录仍在项目本地。
六、综合实战工作流
场景:接手遗留 Express.js 项目
- 全局评估:⌘Shift+F 搜索路由定义 → 选中 → ⌘L 审查一致性
- 批量添加 TypeScript:选中文件 → ⌘K → 添加类型
- 重构路由:⌘I Composer → 提取 inline handler 到 controllers 目录
- 修复 Bug:粘贴代码 → ⌘L 分析 → 应用修复 → 新建 Chat 验证
- 提交与文档:⌘Shift+G → 生成 Commit Message → Chat History 导出到 docs/