Claude Code 深度使用技巧
2026/3/21
Claude Code 深度使用技巧
按需取用,不必全部记住。收集 + 过滤 + 添加自用的 Tips。
🚀 基础设置与快捷操作
自定义状态栏
底部显示模型、目录、Git 分支、未提交文件数、Token 进度条和最后一条消息。支持 10 种颜色主题。
示例:
Opus 4.5 | 📁claude-code-tips | 🔀main (scripts/context-bar.sh uncommitted, synced 12m ago) | ██░░░░░░░░ 18% of 200k tokens
💬 This is good. I don't think we need to change the documentation...
设置方法:参考 context-bar.sh
-c、-r 和 --resume
-c:继续上次会话-r:交互式选择最近会话--resume <session-id>:精确恢复特定会话,session id 在~/.claude/projects/目录下
输入框导航快捷键
Ctrl+A/E:行首/行尾Option+Left/Right:按词跳转Ctrl+W/U/K:删词/删至行首/删至行尾Ctrl+G:在外部编辑器中打开输入框(适合粘贴长文本)Ctrl+V(Mac):粘贴剪贴板图片
设置 cc 别名
# ~/.zshrc
alias cc='claude --dangerously-skip-permissions'
跳过所有权限提示(需了解风险后使用)。
! 前缀直接运行 Bash 命令
输入 !git status 或 !npm test 立即执行,输出会进入上下文供 Claude 分析。
Esc 停止,Esc+Esc 回滚
Esc:立即停止 Claude,不丢失上下文Esc+Esc(或/rewind):打开检查点菜单,支持恢复代码、对话或两者- 注意:Bash 命令执行(如数据库迁移)不在检查点范围内
Ctrl+S 暂存提示词草稿
写到一半想先问个问题?Ctrl+S 暂存草稿,快速提问后自动恢复。
Ctrl+B 后台运行长任务
把测试、构建等耗时操作放到后台,Claude 继续工作,完成后通知你。
/permissions 白名单常用命令
停止对 npm run lint 重复点击确认,白名单信任命令。
设置输出风格
/config 选择 Explanatory(详细步骤)、Concise(简洁)或 Technical(技术精确)风格。
🧠 提示词与上下文管理
不相关任务之间用 /clear
清空上下文比在混乱的长会话中挣扎更有效。
/btw 快速侧问
弹出悬浮层提问,不影响主上下文。
@ 直接引用文件
@src/auth/middleware.ts 精确指向文件,省去 Claude 自己搜索的代价。
用模糊提示探索陌生代码
“这个文件你会改进什么?”
让 Claude 主动发现问题,适合熟悉新代码库。
HANDOFF.md 交接文档
关闭自动压缩,手动让 Claude 写 HANDOFF.md(目标、已试什么、什么有效/无效、下一步),新会话只需提供文件路径即可继续。比 /compact 更可控。
搜索对话历史
对话存于 ~/.claude/projects/,每个会话是 .jsonl 文件。可用 grep 搜索,或直接问 Claude “帮我找今天关于 X 的对话”。
half-clone 减少上下文
half-clone-conversation.sh 只保留对话后半段,比 /branch 更适合”续跑但减负”场景。可配合 Hook 在上下文超 85% 时自动触发。
参考:https://github.com/ykdojo/claude-code-tips/blob/main/skills/half-clone/SKILL.md
Auto Memory 自动记忆
Claude 自动维护 ~/.claude/projects/<project>/memory/MEMORY.md,记录构建命令、调试洞察、项目约定等。比手写 HANDOFF.md 更自动,适合长期项目积累。
📋 CLAUDE.md 配置文件
Claude 犯错后更新 CLAUDE.md
“更新 CLAUDE.md 让这个问题不再发生”
Claude 会自己写规则,下次会话自动遵守。
@imports 保持 CLAUDE.md 精简
用 @docs/git-instructions.md 引用外部文档,Claude 按需读取,不占主文件空间。
CLAUDE.md 是建议,Hooks 是要求
CLAUDE.md 遵守率约 80%,Hooks 是 100% 确定性执行。格式化、lint、安全检查等必须执行的操作用 Hooks。
自动的自动化
发现反复重复的操作就自动化它:CLAUDE.md 记录、skills 封装、脚本化——把元效率做到极致。
⚙️ Hooks 自动化
PostToolUse Hook 自动格式化
每次 Claude 编辑文件后自动运行 Prettier。
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "npx prettier --write \"$CLAUDE_FILE_PATH\""
}
]
}
]
}
}
PreToolUse Hook 阻止危险命令
在 Bash 工具前拦截 rm -rf、drop table、truncate 等破坏性命令。
#!/bin/bash
# .claude/hooks/block-rm.sh
COMMAND=$(jq -r '.tool_input.command')
if echo "$COMMAND" | grep -q 'rm -rf'; then
jq -n '{
hookSpecificOutput: {
hookEventName: "PreToolUse",
permissionDecision: "deny",
permissionDecisionReason: "Destructive command blocked by hook"
}
}'
else
exit 0
fi
Notification Hook 桌面通知
Claude 需要用户注意时触发(如等待权限确认、任务空闲),可配置发送系统通知或声音。
SessionStart Hook 动态注入上下文
会话开始时自动加载特定文件或执行初始化脚本。
🔧 CLI 命令参考
启动与恢复
| 命令 | 说明 |
|---|---|
claude | 启动交互会话 |
claude "query" | 带初始提示启动 |
claude -p "query" | SDK 查询后退出 |
cat file | claude -p "query" | 处理管道内容 |
claude -c | 继续当前目录最近会话 |
claude -r "<session>" "query" | 按 ID/名称恢复会话 |
常用 Flags
| Flag | 说明 |
|---|---|
--dangerously-skip-permissions | 跳过权限提示(慎用) |
--allowedTools | 白名单工具,无需确认 |
--disallowedTools | 禁用特定工具 |
--add-dir | 添加额外工作目录 |
--chrome | 启用 Chrome 浏览器集成 |
--effort low|medium|high|max | 设置努力级别(Opus 4.6) |
--append-system-prompt | 追加系统提示 |
--fork-session | 恢复时创建新会话 ID |
🎯 高级工作流
Plan Mode 分离探索与执行
- Explore:Plan Mode 下读取文件、回答问题,不做修改
- Plan:让 Claude 创建详细实现计划
- Implement:切换 Normal Mode,按计划编码
- Commit:提交并创建 PR
Git Worktrees 并行分支工作
# 创建 worktree
git worktree add ../feature-branch feature-branch
# 在不同 worktree 中运行独立的 Claude Code 实例
cd ../feature-branch && claude
容器中运行高风险任务
docker run -it --rm \
-v $(pwd):/workspace \
-w /workspace \
node:20 \
claude --dangerously-skip-permissions
多 Claude 协作
- 用多个终端标签页
- 每个 Claude 专注不同任务
- 用
git worktree隔离代码变更
📊 监控与调试
/usage 查看 Rate Limits
Current session
█████████▌ 19% used
Resets 12:59am (America/Vancouver)
/stats 查看使用统计
GitHub 风格的活动图、总 token 数、会话数、最长会话、活跃天数等。
/mcp 管理 MCP 服务器
查看已连接的 MCP 服务器状态。
🛡️ 权限管理
权限规则语法
{
"permissions": {
"allow": [
"Bash(npm run lint)",
"Bash(npm run test *)",
"Read(~/.zshrc)"
],
"deny": [
"Bash(curl *)",
"Read(./.env)",
"Read(./secrets/**)"
]
}
}
配置作用域
| 作用域 | 位置 | 共享 |
|---|---|---|
| User | ~/.claude/settings.json | 否 |
| Project | .claude/settings.json | 是(提交到 git) |
| Local | .claude/settings.local.json | 否(gitignore) |
| Managed | 系统级配置 | IT 管理 |
💡 最佳实践
给 Claude 验证工作的方式
最重要的一点:包含测试、截图或预期输出,让 Claude 能自我检查。
| 策略 | 错误示例 | 正确示例 |
|---|---|---|
| 提供验证条件 | ”implement a function that validates email" | "write a validateEmail function. test cases: user@example.com is true, invalid is false. run the tests after implementing” |
| 视觉验证 UI | ”make the dashboard look better" | "[paste screenshot] implement this design. take a screenshot and compare. list differences and fix them” |
| 解决根本原因 | ”the build is failing" | "the build fails with this error: [paste error]. fix it and verify. address root cause, don’t suppress” |
提供具体上下文
| 策略 | 错误示例 | 正确示例 |
|---|---|---|
| 限定范围 | ”add tests for foo.py" | "write a test for foo.py covering the edge case where user is logged out. avoid mocks.” |
| 指向来源 | ”why does ExecutionFactory have weird api?" | "look through ExecutionFactory’s git history and summarize how its api came to be” |
| 引用模式 | ”add a calendar widget" | "look at how existing widgets are implemented. HotDogWidget.php is a good example. follow the pattern.” |
写有效的 CLAUDE.md
- 大小:目标 < 200 行
- 结构:用 markdown headers 和 bullets
- 具体性:写可验证的指令
- 一致性:定期审查,删除冲突规则
| ✅ 包含 | ❌ 排除 |
|---|---|
| Claude 猜不到的 Bash 命令 | Claude 能从代码推断的 |
| 与默认不同的代码风格 | 标准语言约定 |
| 测试指令和偏好 | 详细 API 文档(链接即可) |
| 仓库礼仪(分支命名、PR 约定) | 频繁变化的信息 |
| 项目特有的架构决策 | 教程或长解释 |