Claude Code

Claude code

安装

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# 安装 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

Tools with Claude code

NamePurpose
Read读文件
Edit,MultiEdit编辑现有文件
Write创建文件并写入内容
Bash执行命令
Glob根据模式查找文件/文件夹
Grep搜索内容
Task创建子代理以完成特定任务
WebFetch从URL获取内容并处理
WebSearch搜索网页

基础使用

filepurpose
CLAUDE.md项目级总结,通过/init 生成;和他人共享
CLAUDE.local.md不共享,个人指令
~/claude/CLAUDE.md全局文件,应用所有本地项目的指令

# 开头:进入记忆模式,可以让 Claude 更新 CLAUDE.md @ 提及文件:在记忆模式下也能使用 @ 提及具体文件

ctrl+v :粘贴图片进行提问

两次 Shift+Tab 或者 /plan:进入计划模式,阅读更多文件并进行详细计划

思考模式:提示词中让他多思考就行或者在提示词里加上 ultrathink:

git助手: 直接提交就行

/commit:智能生成提交信息并提交代码

管理上下文

claude -c 恢复之前的对话上下文

claude -r 从历史会话中选择

/export 导出会话为 Markdown

Esc:中断 claude, 允许你重新引导它或给出替代指令

Escape + Escape:回退对话,恢复到之前的消息,移除与当前任务无关的上下文

/compact:清除对话历史但是总结当前对话的所有信息,在新任务时候保持知识连贯

/clear:清空整个对话历史

创建自己的指令

/ : 可以看到很多内置指令

.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服务器添加新工具和功能

.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 # 移除配置

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
claude mcp add codex -s user -- codex -m gpt-5.1-codex-max -c model_reasoning_effort="high" mcp-server
claude mcp remove codex # 移除配置

hook

调用工具的前后操作

  1. 决定使用PreToolUse(工具使用前)或PostToolUse(工具使用后)钩子
  2. 确定需要监控的工具调用类型
  3. 编写接收工具调用的命令
  4. 如有需要,该命令应向Claude提供反馈

matcher: 匹配需要监控的工具;command: 尝试调用匹配的工具时,要运行的命令

ScopePath
Global~/.claude/settings.json
Project.claude/settings.json
Project (不提交).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

 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."

参考阅读

claude-code-in-action

Claude Code: A Highly Agentic Coding Assistant

Claude Code overview - Claude Code Docs

0%