精选 Claude Code CLI AI 编程工具 常用指令与核心速查备忘单,涵盖高频用法、配置参数与实用技巧。
paths:
所有 API 端点必须包含输入合法性校验。
没有 `paths` 的规则在每次会话均加载。带有 `paths` 的规则仅在打开匹配的文件时加载。
### 自动记忆
Claude 会将自己的学习笔记保存在:
~/.claude/projects/
- 默认开启;可在 `/memory` 中切换或设置 `autoMemoryEnabled: false`
- 跨同一 git 仓库的会话和 worktree 保持持久化
- 本地机器专属——不会跨机器共享
通过环境变量禁用:
```bash
$ export CLAUDE_CODE_DISABLE_AUTO_MEMORY=1
从 CLAUDE.md 引用其他文件:
项目概述请参阅 @README.md。
构建命令参考:@package.json
# 工作流规范
@docs/git-workflow.md
| 事件名称 | 触发时机 |
|---|---|
SessionStart |
会话启动、恢复或清空时 |
UserPromptSubmit |
用户提交 Prompt 提示词时 |
PreToolUse |
工具开始执行前 |
PermissionRequest |
弹出权限对话框前 |
PostToolUse |
工具成功执行完成后 |
PostToolUseFailure |
工具执行失败后 |
PermissionDenied |
自动模式分类器拒绝操作时 |
Stop |
Claude 完成响应回答时 |
StopFailure |
发生 API 错误导致轮次结束时 |
Notification |
Claude 需要用户关注介入时 |
SubagentStart |
子代理 (Subagent) 被创建启动时 |
SubagentStop |
子代理完成退出时 |
FileChanged |
监听的文件在磁盘上被修改时 |
PreCompact |
上下文压缩开始前 |
PostCompact |
上下文压缩完成后 |
WorktreeCreate |
创建 Worktree 工作树时 |
WorktreeRemove |
移除 Worktree 工作树时 |
InstructionsLoaded |
加载 CLAUDE.md 文件时 |
ConfigChange |
配置文件修改变动时 |
TaskCreated |
通过 TaskCreate 创建任务时 |
TaskCompleted |
任务标记完成时 |
退出码说明:0 = 成功(JSON 决策),2 = 拦截阻止操作(stderr 传给模型),其他 = 非阻塞性错误。
使用 $CLAUDE_PROJECT_DIR 引用相对于项目根目录的挂钩脚本。
| 类型 | 详细描述 |
|---|---|
command |
运行 Shell 脚本,从 stdin 读取 JSON 数据 |
http |
向 HTTP 端点发送 POST JSON 请求 |
prompt |
请求 Claude 做出 是/否 决策判定 |
agent |
创建子代理并配合工具进行验证校验 |
文件修改后自动格式化 (Auto-format)
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"hooks": [
{
"type": "command",
"command": "npm run lint --silent"
}
]
}
]
}
}
需要介入时通知提示 (macOS)
{
"hooks": {
"Notification": [
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "osascript -e 'display notification \"Claude needs attention\" with title \"Claude Code\"'"
}
]
}
]
}
}
配置文件存储在 ~/.claude/settings.json (用户) 或 .claude/settings.json (项目)。使用 /hooks 查看。
Stdio 本地进程服务器
$ claude mcp add my-server -- npx my-mcp-server
带环境变量配置
$ claude mcp add -e KEY=val my-server -- npx server
HTTP 远程服务器
$ claude mcp add --transport http sentry https://mcp.sentry.dev/mcp
带身份验证头部的 HTTP 服务器
$ claude mcp add --transport http my-api https://api.example.com/mcp --header "Authorization: Bearer $TOKEN"
JSON 格式添加
$ claude mcp add-json my-server '{"command":"npx","args":["my-mcp"]}'
从 Claude Desktop 导入
$ claude mcp add-from-claude-desktop
服务器管理
$ claude mcp list # 列出所有已配置的服务器
$ claude mcp get <name> # 查看指定服务器的详细信息
$ claude mcp remove <name> # 移除指定服务器
配置存储在 ~/.claude/settings.json (用户) 或 .claude/settings.json (项目)。
使用 @ 引用 MCP 资源:
@github:repos/owner/repo/issues
@memory:entities
@filesystem:/path/to/file
将 MCP 提示词作为斜杠指令运行:
/mcp__github__search_repositories
/mcp__memory__create_entities
MCP 工具命名模式:mcp__<server>__<tool>
在会话中管理:
/mcp
针对 MCP 工具添加挂钩:
{
"hooks": {
"PreToolUse": [
{
"matcher": "mcp__memory__.*",
"hooks": [{ "type": "command", "command": "logger.sh" }]
}
]
}
}
在隔离分支中运行并行会话:
# 创建 worktree 并启动 Claude
$ claude --worktree feature-auth
$ claude -w bugfix-123
# 自动生成名称
$ claude --worktree
Worktrees 创建在 .claude/worktrees/<name>/ 并从 origin/HEAD 派生。
自动清理规则:无代码修改 → 退出时自动删除;有代码修改 → Claude 会询问保留还是删除。
在 .gitignore 中忽略:
.claude/worktrees/
复制密钥至 Worktrees
在项目根目录创建 .worktreeinclude:
.env
.env.local
config/secrets.json
手动 Worktree 命令
# 基于指定分支创建
$ git worktree add ../project-feat -b feat
# 列出所有 worktrees
$ git worktree list
# 完成后移除
$ git worktree remove ../project-feat
远程分支变更后同步更新 origin HEAD:
$ git remote set-head origin -a
在 .claude/agents/<name>.md 中创建:
---
name: code-reviewer
description: 审查代码中的 Bug 与代码风格
tools:
- Read
- Grep
- Glob
---
你是一位资深代码审查专家。
请重点关注代码的正确性与清晰度。
在对话中自然使用子代理:
review the auth module for security issuesuse the code-reviewer agent on api.ts/agents — 管理代理配置带 Worktree 隔离的子代理:
---
name: parallel-worker
isolation: worktree
description: 在独立分支中工作
---
子代理共享自动记忆 — 在 frontmatter 中添加:
autoMemoryEnabled: true
使用 -p / --print 选项通过脚本运行 Claude:
# 基础单次查询
$ claude -p "what does this file do?"
# 串流 JSON (实时事件流)
$ claude -p "query" --output-format stream-json
# 带完整元数据的 JSON
$ claude -p "query" --output-format json
# 限制轮次与消费预算
$ claude -p "query" --max-turns 5 --max-budget-usd 1.00
# 校验结构化输出
$ claude -p "query" --json-schema '{"type":"object"}'
无头模式下恢复会话
# 继续最近一次的会话
$ claude -c -p "follow-up question"
# 恢复具名会话
$ claude -r my-session -p "continue the task"
输出格式对比
| 格式名称 | 适用场景 |
|---|---|
text |
适合人类阅读 (默认) |
json |
包含完整元数据与成本费用信息 |
stream-json |
适合逐轮实时事件监听 |
Bare 极简模式 (极速启动)
$ claude --bare -p "query"
跳过 hooks、CLAUDE.md 自动搜寻、插件、MCP 和自动记忆。自动设置 CLAUDE_CODE_SIMPLE=1。非常适合脚本自动化调用。
会话持久化配置
# 不将会话保存至磁盘
$ claude -p "query" --no-session-persistence
创建云端定时任务:
/schedule review open PRs every morning at 9am
会话级循环常驻任务:
/loop 5m check for new issues
GitHub Actions 集成
- uses: anthropics/claude-code-action@beta
with:
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
prompt: 'review this PR for issues and comment'
GitLab CI/CD 集成
claude-review:
script:
- claude -p "review changes in this MR"
--output-format json
| 计划任务选项 | 运行环境 |
|---|---|
| 云端定时任务 (Cloud) | Anthropic 云端基础设施 |
| 桌面定时任务 (Desktop) | 您的本地计算机 |
| GitHub Actions | CI/CD 流水线 |
/loop |
仅限当前 CLI 会话期间 |