Claude code
安装
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 安装 nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
source ~/.bashrc
nvm --version # 验证 nvm 安装
nvm install --lts
node --version
npm --version
# 安装 claude-code
npm install -g @anthropic-ai/claude-code
# 验证
which claude
claude --help
# 更新
claude update
Name Purpose Read 读文件 Edit,MultiEdit 编辑现有文件 Write 创建文件并写入内容 Bash 执行命令 Glob 根据模式查找文件/文件夹 Grep 搜索内容 Task 创建子代理以完成特定任务 WebFetch 从URL获取内容并处理 WebSearch 搜索网页
基础使用
file purpose CLAUDE.md 项目级总结,通过/init 生成;和他人共享 CLAUDE.local.md 不共享,个人指令 ~/claude/CLAUDE.md 全局文件,应用所有本地项目的指令
# 开头:进入记忆模式,可以让 Claude 更新 CLAUDE.md
@ 提及文件:在记忆模式下也能使用 @ 提及具体文件
ctrl+v :粘贴图片进行提问
两次 Shift+Tab 或者 /plan:进入计划模式,阅读更多文件并进行详细计划
思考模式:提示词中让他多思考就行或者在提示词里加上 ultrathink:
git助手: 直接提交就行
/commit:智能生成提交信息并提交代码
!command:执行命令,例如:!ls
/loop: 让claude重复执行指令
ctrl + g:在编辑器中打开计划,编辑后保存,claude会继续执行计划
ctrl + o: 打开详细的操作历史记录
ctrl + s: 将当前未完成输入暂存,方便后续使用,避免删改焦虑
claude -w worktree_name: 使用工作树(worktree)功能创建新工作区,后续可以进行合并主分支比如:mege back to main
claude 提示词 -p: 直接在命令行输入提示词,无需进入TUI,适合脚本集成和后台自动化
管理上下文
claude -c 恢复之前的对话上下文 claude --continue
claude -r 从历史会话中选择 claude --resume
/export 导出会话为 Markdown
Esc:中断 claude, 允许你重新引导它或给出替代指令
Escape + Escape:回退对话,恢复到之前的消息,移除与当前任务无关的上下文 /rewind
/compact:清除对话历史但是总结当前对话的所有信息,在新任务时候保持知识连贯
/clear:清空整个对话历史
/btw: by the way 对于不需要上下文的快速问题,不会进入对话记录 ,不会污染推理链
创建自己的指令
/ : 可以看到很多内置指令
.claude/commands/audit.md 文件名就是指令,里面内容是指令的操作步骤,重启后就能使用/audit.下面文档就是示例。
1
2
3
4
5
6
7
Your goal is to update any vulnerable dependencies.
Do the following:
1.Run `npm audit` to find vulnerable installed packages in this project
2.Run `npm audit fix` to apply updates
3.Run tests and verify the updates didn't break anything
MCP
Connect Claude Code to tools via MCP
.claude/settings.local.json 里配置权限
1
2
3
4
5
6
{
"permissions" : {
"allow" : [ "mcp__playwright" ],
"deny" : []
}
}
1
2
3
4
claude mcp list # 查看当前配置的 MCP 服务器
claude mcp add playwright npx @playwright/mcp@latest # 安装 playwright 服务,Playwright 提供控制浏览器的能力
# Open the browser and navigate to localhost:3000
claude mcp remove playwright # 移除配置
1
2
3
# codex cli安装好之后配置mcp
claude mcp add codex -s user -- codex -m gpt-5.1-codex-max -c model_reasoning_effort = "high" mcp-server
claude mcp remove codex # 移除配置
Hooks
Hooks reference
调用工具的前后操作
决定使用PreToolUse(工具使用前)或PostToolUse(工具使用后)钩子 确定需要监控的工具调用类型 编写接收工具调用的命令 如有需要,该命令应向Claude提供反馈 matcher: 匹配需要监控的工具;command: 尝试调用匹配的工具时,要运行的命令
Scope Path Global ~/.claude/settings.jsonProject .claude/settings.jsonProject (不提交) .claude/settings.local.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{
"hooks" : {
"PreToolUse" : [
{
"matcher" : "Read|Grep" ,
"hooks" : [
{
"type" : "command" ,
"command" : "node ./hooks/read_hook.js"
}
]
}
],
"PostToolUse" : [
{
"matcher" : "Write|Edit|MultiEdit" ,
"hooks" : [
{
"type" : "command" ,
"command" : "true"
}
]
}
]
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// ./hooks/read_hook.js
async function main () {
const chunks = [];
for await ( const chunk of process . stdin ) {
chunks . push ( chunk );
}
const toolArgs = JSON . parse ( Buffer . concat ( chunks ). toString ());
const readPath =
toolArgs . tool_input ? . file_path || toolArgs . tool_input ? . path || "" ;
// TODO: 让claude不要读隐私文件,比如.env
if ( readPath . includes ( '.env' )) {
console . error ( "You cannot read the .env file" );
process . exit ( 2 );
}
}
main ();
claude code sdk
claude-agent-sdk-python
1
2
3
4
5
6
7
8
9
10
11
12
import anyio
from claude_code_sdk import query
async def main ():
prompt = "Hello, Claude! Tell me a joke."
async for message in query ( prompt ):
print ( message )
anyio . run ( main )
# cli: claude -p "Hello, Claude! Tell me a joke."
plugin
Create plugins
1
2
3
4
5
6
my-plugin/
├── .claude-plugin/
│ └── plugin.json
└── skills/
└── code-review/
└── SKILL.md
Skills
Extend Claude with skills 可重复使用的说明、知识或工作流程
Agent Skills 标准
1
2
3
/plugin list # 列出插件
/plugin uninstall <plugin-name> # 卸载插件
/plugin marketplace remove <marketplace-name> # 移除市场
创建自己的skill
/skill-creator 该skill可以根据好的提示词、实践(基于之前的操作)、想法创建skill。
基于提示词创建 :提供现成的提示词,帮我将其封装为一个可复用的 skill基于操作过程创建 :将刚刚完成的一系列操作流程,固化成一个可复用的 skill基于想法创建 :我有一个关于 X 的初步想法,请通过提问帮我梳理需求,最终生成对应的 skill位置 路径 Applies to 适用于 Personal ~/.claude/skills/<skill-name>/SKILL.md所有项目 Project .claude/skills/<skill-name>/SKILL.md当前项目 Plugin <plugin>/skills/<skill-name>/SKILL.md插件被启用
.claude/skills/explain-code 文件夹,每个技能都是一个目录,SKILL.md 作为入口:
.claude/skills/explain-code/SKILL.md:分为两部分
Yaml前置标记 descriptio字段:何时使用该技能和 name字段:名字,disable-model-invocation:可选,以防止 Claude 自动触发。还支持技能内容中动态值的字符串替换,比如能传递参数 $ARGUMENTS。
markdown内容包含技能调用要遵循的指令。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
---
name: explain-code
description: Explains code with visual diagrams and analogies. Use when explaining how code works, teaching about a codebase, or when the user asks "how does this work?"
disable-model-invocation: true # 禁止自动调用
allowed-tools: ["Read", "Grep"] # 限制可用工具
context: fork # 在子代理中运行
---
When explaining code, always include:
1. **Start with an analogy** : Compare the code to something from everyday life
2. **Draw a diagram** : Use ASCII art to show the flow, structure, or relationships
3. **Walk through the code** : Explain step-by-step what happens
4. **Highlight a gotcha** : What's a common mistake or misconception?
Keep explanations conversational. For complex concepts, use multiple analogies.
工具包装器(Tool Wrapper)
目的 :让 Agent 成为特定库的“即时专家”,按需加载上下文。机制 :在 SKILL.md 中监听关键词,动态从 references/ 目录加载内部文档,并强制将其作为绝对真理应用。适用 :分发团队编码规范、框架最佳实践。生成器(Generator)
目的 :确保输出结构一致,解决 Agent 每次生成文档结构不同的问题。机制 :使用 assets/ 存放模板,references/ 存放风格指南;Agent 像项目经理一样加载模板、读取风格、向用户询问缺失变量,最后填充输出。适用 :生成可预测的 API 文档、标准化提交信息、项目脚手架等。审查器(Reviewer)
目的 :将“检查什么”与“如何检查”分离,实现模块化代码审查。机制 :将审查清单放在 references/review-checklist.md 中,Agent 加载清单后按严重程度(错误、警告、提示)逐条评分。适用 :自动化 PR 审查、安全漏洞扫描等,替换清单即可获得不同的专项审计。反转模式(Inversion)
目的 :让 Agent 先扮演采访者,通过多轮提问收集完整需求后再行动,避免猜测和过早生成。机制 :使用明确的“门控指令”(如“在完成所有阶段前禁止构建”),强制 Agent 按顺序提问,收集完所有答案后才进入最终合成阶段。适用 :需求模糊、需要充分上下文后才能开始的任务。流水线模式(Pipeline)
目的 :对复杂任务强制执行严格的多步顺序工作流,设置硬性检查点,防止跳步或忽略指令。机制 :SKILL.md 本身定义工作流,强制顺序执行。各步骤按需加载对应的参考文件或模板,保持上下文窗口整洁。适用 :不允许跳过任何步骤的复杂任务,如生成 API 文档、部署流水线等。Subagent
Create custom subagents 每个子代理在自己的上下文窗口中运行,拥有自定义系统提示、特定工具访问和独立权限
.claude/agents/ 子代理,或者用claude自动生成。
生成器-验证器 (Generator-verifier)
目的 :确保质量关键输出的准确性和一致性,适用于有明确评估标准的任务。机制 :生成器生产初稿,校验器依据显式的评判标准进行审核。若不合格,反馈回生成器修改,形成循环迭代,直至通过或达最大重试次数。适用 :代码生成与测试、合规性检查、事实核查、邮件起草等有明确对错标准的场景。编排器-子智能体 (Orchestrator-subagent)
目的 :通过分层架构清晰分解复杂任务,由“队长”统筹全局并整合结果。机制 :编排器负责规划工作、委派任务并综合结果。子智能体负责负责具体职责并汇报,在独立上下文中执行单一、有边界的任务后返回结果,生命周期较短。适用 :任务分解边界清晰、子任务相互独立且耗时较短的场景(如代码审查、多维度报告生成)。智能体团队 (Agent teams)
目的 :处理需要长时间运行、且能从持续积累的上下文中获益的并行子任务 。机制 :协调器派发任务后,工作智能体持续存活,在多个步骤中自主认领队列任务,不断积累领域经验,完成后上报结果。适用 :大型代码库迁移、独立服务重构等需要跨越多轮交互、且各单元互不干扰的长线任务。消息总线 (Message bus)
目的 :在智能体生态系统不断扩张时,通过事件驱动解耦依赖,实现灵活、可扩展的响应式工作流。机制 :智能体向总线发布 事件或订阅 感兴趣的主题。路由器负责分发消息,新加入的智能体无需修改旧代码即可接入工作流。适用 :安全运维自动化、事件驱动的数据处理管道,以及需要动态路由而非硬编码顺序的场景。共享状态 (Shared-state)
目的 :消除中央协调瓶颈,让智能体通过公共存储直接协同发现与增量构建知识。机制 :所有智能体直接读写同一个持久化存储区 (数据库/文档),彼此可见对方的实时发现,无需中介转发。适用 :学术或市场研究综合、需要多方实时启发与成果交叉引用的协作任务,或要求无单点故障的系统。常用插件推荐
选一个就行,避免功能冲突。
superpowers :工作流导向插件,内置 test-driven-development(TDD)和 brainstorming 等技能,规范开发流程
get-shit-done
everything-claude-code
Ralph
实践
Best Practices for Claude Code
Explore first, then plan, then code
1
2
3
4
5
6
7
#1. 计划模式 进行探索:Claude 阅读文件并回答问题,无需更改文件。
#2. Create a plan. 按 Ctrl+G 在文本编辑器中打开计划,直接编辑,然后 Claude 继续。
I want to add xxxx. What files need to change? What' s the session flow? Create a plan.
#3. 切回普通模式,让claude 编码,并对照计划进行验证。
implement the xxxx from your plan. write tests for the callback handler, run the test suite and fix any failures.
#4. commit提交
commit with a descriptive message and open a PR
Claude Code Cheat Sheet
Claude Code Cheat Sheet
使用Github Copilot订阅
1
npx @jeffreycao/copilot-api@latest start
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
"env" : {
"ANTHROPIC_BASE_URL" : "http://localhost:4141" ,
"ANTHROPIC_AUTH_TOKEN" : "dummy" ,
"ANTHROPIC_MODEL" : "gpt-5.4" ,
"ANTHROPIC_DEFAULT_SONNET_MODEL" : "gpt-5.4" ,
"ANTHROPIC_DEFAULT_HAIKU_MODEL" : "gpt-5-mini" ,
"DISABLE_NON_ESSENTIAL_MODEL_CALLS" : "1" ,
"CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC" : "1" ,
"CLAUDE_CODE_ATTRIBUTION_HEADER" : "0" ,
"CLAUDE_CODE_ENABLE_PROMPT_SUGGESTION" : "false"
} ,
"permissions" : {
"deny" : [
"WebSearch"
]
}
Github Copliot Chat vscode 插件快捷键ctrl+alt+i,打开session
参考阅读
claude-code-in-action
Claude Code: A Highly Agentic Coding Assistant
Claude Code overview - Claude Code Docs
Use Cases | Claude
Claude Code interactive
Claude Code Unpacked
Codex
安装
1
2
3
4
# 安装 Codex CLI,并能在终端调用 codex 命令
npm install -g @openai/codex
# 配置~/.codex/auth.json 输入codex进去后填入apikey就会自动生成
# 配置 ~/.codex/config.toml
1
2
3
4
5
6
7
8
9
10
11
# ~/.codex/config.toml 使用自己的提供商
model_provider = "codex-for-me"
model = "gpt-5.2-codex"
model_reasoning_effort = "high"
disable_response_storage = true
[ model_providers . codex-for-me ]
name = "codex-for-me"
base_url = "https://api-vip.codex-for.me/v1"
wire_api = "responses"
requires_openai_auth = true
1
2
npm update -g @openai/codex # 更新
npm uninstall -g @openai/codex # 卸载
参考阅读
Codex | OpenAI Developers
Best practices – Codex | OpenAI Developers
OpenCode
安装
1
npm i -g opencode-ai@latest
/connect and select GitHub Copilot 连入即可。
使用oh my opencode
oh-my-opencode
参考阅读
OpenCode | GitHub