# 方式1:原生安装(无需 Node.js)
curl -fsSL https://claude.ai/install.sh | bash
# 安装后 ~/.local/bin/claude 加入 PATH
# export PATH="$HOME/.local/bin:$PATH"
# 方式2:Homebrew
brew install --cask claude-code
# PowerShell
irm https://claude.ai/install.ps1 | iex
# CMD
curl -fsSL https://claude.ai/install.cmd -o install.cmd && install.cmd && del install.cmd
# WinGet
winget install Anthropic.ClaudeCode
curl -fsSL https://claude.ai/install.sh | bash
claude --version
> 国内直连 Anthropic 不稳定,推荐用 CC Switch 接入 DeepSeek API,成本极低。
brew tap farion1231/ccswitch
brew install --cask cc-switch
或下载 DMG 手动安装:https://github.com/farion1231/cc-switch/releases
| 配置项 | 值 |
|---|---|
| -------- | ----- |
| 名称 | DeepSeek V4 Pro |
| API Key | 从 https://platform.deepseek.com 获取(sk- 开头) |
| Base URL | https://api.deepseek.com/anthropic |
| Opus 模型 | deepseek-v4-pro |
| Sonnet 模型 | deepseek-v4-pro |
| Haiku 模型 | deepseek-v4-flash |
~/.claude/settings.json| 模型 | 输入(缓存命中) | 输入(未命中) | 输出 |
|---|---|---|---|
| ------ | ---------------- | ---------------- | ------ |
| deepseek-v4-flash | $0.0028/1M | $0.14/1M | $0.28/1M |
| deepseek-v4-pro | $0.0036/1M | $0.435/1M | $0.87/1M |
首次运行 claude 需要 OAuth 登录(用邮箱注册即可,付费订阅非必需——CC Switch 会拦截请求转到 DeepSeek):
claude
# 浏览器自动打开,完成邮箱登录
# 登录后终端显示会话启动成功
| 命令 | 说明 |
|---|---|
| ------ | ------ |
claude | 交互模式 |
claude "任务描述" | 单次任务模式,完成后自动退出 |
claude -p "查询内容" | 打印模式,结果输出到 stdout |
claude -c | 继续上一次会话 |
claude -r | 会话恢复选择器 |
| 命令 | 说明 |
|---|---|
| ------ | ------ |
/help | 显示所有命令 |
/clear | 清空对话上下文 |
/compact | 压缩上下文节省 Token |
/model | 切换模型 |
/cost | 显示 Token 用量和费用 |
/login | 切换账号 |
/init | 生成 CLAUDE.md 项目配置 |
/review | 审查最近代码变更 |
/doctor | 诊断安装/运行问题 |
/agents | 管理子代理 |
/hooks | 配置 Hooks |
| 快捷键 | 操作 |
|---|---|
| -------- | ------ |
Tab | 自动补全 |
↑ | 命令历史 |
Ctrl+O | 切换深度思考模式 |
Ctrl+C | 取消当前操作 |
每次会话自动读取,直接决定输出质量。
<项目根目录>/CLAUDE.md(项目技术栈、编码规范)~/.claude/CLAUDE.md(个人偏好,跨项目生效)## 项目概述
- 技术栈:TypeScript + React + Node.js
- 包管理器:npm
- 测试框架:Vitest
## 编码规范
- 使用 TypeScript 严格模式
- 函数必须有 JSDoc 注释
- 不使用 any 类型
## 常用命令
- 启动:npm run dev
- 测试:npm test
- 构建:npm run build
## 禁止操作
- 不要修改 package.json 中的依赖版本
- 不要提交未通过测试的代码
claude
> /init # 自动扫描项目生成初始 CLAUDE.md
# 然后手动补充上述内容
# 简单函数
create a utility function that validates email addresses using regex,
include JSDoc and edge cases
# 多文件功能(跨层实现)
implement user notification feature with:
1. database table migration
2. API endpoint in src/routes/
3. frontend component in src/components/
# 参考现有模式(引用文件)
@src/routes/users.ts - follow this pattern to implement /api/products
# 粘贴错误信息(保留英文原文)
paste the full error message here
# 自动修复测试
run npm test, find failing tests, and fix them
# 审查上次提交是否引入 Bug
review the changes in my last commit for potential bugs
# 智能提交(自动生成语义化 commit message)
claude commit
# 创建 PR(自动汇总变更)
claude
> create a pr
# 处理 PR 审查评论
> /pr-comments
在项目根目录创建 .mcp.json:
{
"mcpServers": {
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": { "GITHUB_TOKEN": "your-token" }
},
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem"],
"args": ["/path/to/allowed/dir"]
}
}
}
配置方式:交互模式中执行 /hooks
常用事件:
permission_prompt:Claude 请求权限时触发idle_prompt:Claude 等待用户输入时触发示例(macOS 通知):
osascript -e 'display notification "Claude needs attention" with title "Claude Code"'
在 .claude/commands/ 下创建 .md 文件:
# .claude/commands/review-pr.md
Review the current PR diff. Focus on:
1. Security issues
2. Performance problems
3. Code style violations
创建后可通过 /review-pr 调用。
在 .claude/agents/ 下创建 JSON 配置文件,定义专门代理处理复杂任务。
管理:/agents 查看和配置。
use plan mode to review the authentication architecture
此模式下 Claude 只能读取文件和提问,不能修改文件或执行命令。
claude --worktree feature-auth
claude --worktree bugfix-123
每个 worktree 有独立分支和工作目录,互不干扰。
在 .claude/settings.json 中预配置权限,跳过安全命令审批:
{
"permissions": {
"allow": [
"Read",
"Glob",
"Grep",
"Bash(npm test)",
"Bash(npm run lint)",
"Bash(git *)"
],
"deny": [
"Bash(rm -rf *)",
"Bash(curl *)"
]
}
}
权限审批选项:
y:仅本次允许n:拒绝,Claude 尝试替代方案a:当前会话始终允许d:永久拒绝该类操作| 问题 | 解决方案 |
|---|---|
| ------ | ----------- |
command not found: claude | 添加 ~/.local/bin 到 PATH:export PATH="$HOME/.local/bin:$PATH" |
| 响应缓慢 | 执行 /compact 压缩上下文,或 /clear 清空 |
| Claude 改动超出预期 | git reset 回滚,重写更窄范围的 Prompt |
| 命令被拒绝执行 | 检查 .claude/settings.json 权限配置 |
| MCP 服务器启动失败 | 手动运行 MCP 命令检查依赖 |
| CC Switch 配置后无效果 | 完全退出 Claude Code 重新启动 |
/model 看不到目标模型 | 检查 CC Switch 模型槽位配置 |
claude
> /doctor # 输出自动诊断报告
# ✅ 好:具体、有边界、有验收标准
Refactor the error handling in src/services/payment.ts:
- Replace all throw statements with Result type
- Maintain backward compatibility
- Add tests for all new error cases
- Run npm test to verify before finishing
# ❌ 差:模糊、无边界
fix the error handling
用 @ 前缀引用文件,Claude 自动加载内容作为上下文:
Explain the logic in @src/utils/auth.js
Compare @src/routes/users.ts vs @src/routes/orders.ts error handling
共 1 个版本