朋友们,别再问了,Claude Code到底怎么用?今天来一份“全链路生存指南”,不藏私,细到毛孔,啰嗦到你嫌烦。你要的不是“入门”,是“活下去”,是“用到极致”!话糙理不糙,能落地。
别把它当成又一个AI IDE。Claude Code是命令行里的AI小机灵鬼,是你深夜debug时唯一的朋友。和那些花里胡哨的IDE比,它有这些硬核优势:
核心痛点 | 传统AI IDE | Claude Code (CLI) |
---|---|---|
AI能力 | 被阉割,回答质量差 | 原生Claude 4 Sonnet/Opus,聪明到飞起 |
工具调用 | 25次就掐断 | 无限调用,想咋用咋用 |
上下文 | 窗口小,项目大就懵 | 200K+上下文,项目再大都能装下 |
Agent能力 | 任务一长就掉线 | 完全自主,能自己干到底 |
调试 | 只能看代码 | 直接读系统日志,查问题一针见血 |
Claude官方不支持中国大陆用户,推荐用国内镜像站。两种方式都能用,命令全给你写明白。
https://aicodewith.com/?invitation=EK1S5F
curl -fsSL https://aicodewith.com/claudecode/resources/install-script-macos | bash
curl -fsSL https://aicodewith.com/claudecode/resources/install-script-linux | bash
wsl --install
curl -fsSL https://aicodewith.com/claudecode/resources/install-script-wsl | bash
npm install -g @anthropic-ai/claude-code
claude --version
API Key配置(镜像站不用管,官方装的要去官网拿key)
export ANTHROPIC_API_KEY="sk-your-key-here"
echo 'export ANTHROPIC_API_KEY="sk-your-key-here"' >> ~/.bashrc && source ~/.bashrc
echo 'export ANTHROPIC_API_KEY="sk-your-key-here"' >> ~/.zshrc && source ~/.zshrc
echo 'set -gx ANTHROPIC_API_KEY "sk-your-key-here"' >> ~/.config/fish/config.fish
基本配置
claude config set -g model claude-sonnet-4
claude config set -g verbose true
claude config set -g outputFormat text
claude "Hello, Claude!"
或 claude /doctor
安全设置(有风险,自行选择)
export DISABLE_TELEMETRY=1
export DISABLE_ERROR_REPORTING=1
export DISABLE_NON_ESSENTIAL_MODEL_CALLS=1
claude config set allowedTools "Edit,View"
claude config set hasTrustDialogAccepted
claude config set hasCompletedProjectOnboarding
claude config set ignorePatterns
claude config set --global
claude
claude "帮我修复这个 bug"
claude -p ""
cat file | claude -p ""
claude update
claude mcp
claude -c
claude -r
claude --resume
claude --resume
/help
:列出所有命令/add-dir
:加更多工作目录/bug
:报错/clear
:清除聊天记录/compact
:压缩上下文/config
:配置菜单/cost
:token花费统计/doctor
:完整性检查/exit
:退出/init
:初始化项目,生成CLAUDE.md/mcp
:看MCP列表和状态/memory
:编辑记忆/model
:换模型/permissions
:改工具权限/pr_comments
:看PR评论/review
:请求代码审查/sessions
:列出sessions/status
:系统/账户状态/terminal-setup
:装Shift+Enter绑定/vim
:切换vim模式MCP让Claude能连外部服务、数据库、API、工具,功能直接拉满。
claude mcp list
claude mcp add
claude mcp remove
~/.claude.json
Git MCP
npm install -g git-mcp-server
claude mcp add git "git-mcp-server"
claude mcp add github "github-mcp-server --token $GITHUB_TOKEN"
数据库MCP
npm install -g postgres-mcp-server
npm install -g mysql-mcp-server
npm install -g sqlite-mcp-server
export POSTGRES_URL="postgresql://user:password@localhost:5432/mydb"
claude mcp add postgres "postgres-mcp-server --url $POSTGRES_URL"
MCP工具权限
claude --allowedTools "mcp__git__commit,mcp__git__push"
claude --allowedTools "mcp__postgres__*"
claude --allowedTools "Edit,View,mcp__git__*"
~/.claude.json
,全局生效{
"model": "claude-sonnet-4",
"verbose": true,
"outputFormat": "text",
"allowedTools": ["Edit", "View"],
"disallowedTools": []
}
settings.json
或类似名,只在项目目录生效{
"model": "claude-sonnet-4",
"systemPrompt": "You are a senior developer working on this project",
"allowedTools": [
"Edit",
"View",
"Bash(git:)",
"Bash(npm:*)"
]
}
变量名 | 默认值 | 作用 |
---|---|---|
DISABLE_NON_ESSENTIAL_MODEL_CALLS | 0 | 跳过自动摘要/背景解释/git diff,省token |
MAX_THINKING_TOKENS | ≈30‑40k | 最大思考token花费 |
DISABLE_TELEMETRY | 0 | 是否发统计和错误日志 |
CLAUDE_CODE_USE_BEDROCK | 0 | 用AWS Bedrock验证账号 |
CLAUDE_CODE_USE_VERTEX | 0 | 用Google Vertex AI验证账号 |
HTTP_PROXY | unset | HTTP代理 |
HTTPS_PROXY | unset | HTTPS代理 |
NO_PROXY | localhost | 绕过代理的主机/IP |
级别 | 描述 | 风险 |
---|---|---|
Interactive | 每次操作都问 | 低 |
Allowlist | 只让用指定工具 | 中 |
Dangerous | 全部放开 | 高 |
claude --allowedTools "Edit,View"
claude --allowedTools "Edit,View,Bash"
claude --allowedTools "Bash(git:*)"
claude --allowedTools "Bash(git:*),Bash(npm:*)"
claude --dangerously-skip-permissions
限制具体工具权限
claude --allowedTools "Edit,View,Bash(git:status)"
claude --allowedTools "Bash"
保护敏感数据
export DATABASE_URL="postgresql://user:pass@host/db"
claude "connect to postgresql://user:password123@host/db"
定期审查权限组
claude config get allowedTools
claude config get disallowedTools
claude config list
通过prompt关键字控制Claude的“思考深度”:
深度 | 关键词 |
---|---|
1 | think |
2 | think about it, think a lot, think deeply, think hard, think more, megathink |
3 | think harder |
例子:
claude -p "We have a tricky concurrency bug. ultrathink and propose a fix."
(关键字随便放哪,最高优先级生效)
命令 | 功能 | 用例 |
---|---|---|
claude config list | 显示所有设置 | claude config list |
claude config get | 查具体设置 | claude config get theme |
claude config set -g | 全局设置 | claude config set -g theme dark |
claude config add -g | 数组类型加值 | claude config add -g env CLAUDE_CODE_ENABLE_TELEMETRY=1 |
claude config remove -g | 数组类型删值 | claude config remove -g env CLAUDE_CODE_ENABLE_TELEMETRY |
常用key举例:
Key | 常用值 | 用例 | 说明 |
---|---|---|---|
apiKeyHelper | 脚本路径 | claude config set -g apiKeyHelper ~/.claude/key_helper.sh | 自动换API key |
installMethod | npm | claude config set -g installMethod npm | 安装方式 |
autoUpdates | true/false | claude config set -g autoUpdates false | 自动更新开关 |
theme | dark | claude config set -g theme dark | 主题 |
verbose | true | claude config set -g verbose true | 输出详细 |
… | … | … | … |
每次PR自动审查,自动部署,Claude帮你把关。
核心配置:
name: Claude Code Review
on:
pull_request:
branches: [main, develop]
jobs:
claude-review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node.js 18
uses: actions/setup-node@v4
with:
node-version: '18'
- name: Install Claude Code
run: npm install -g @anthropic-ai/claude-code
- name: Review PR
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: |
claude -p "Review changes for security issues and bugs" \
--allowedTools "View" \
--output-format json > review-results.json
- name: Upload results artifact
uses: actions/upload-artifact@v4
with:
name: claude-review
path: review-results.json
关键点:
让Claude帮你在commit前查大坑,关键代码如下:
#!/usr/bin/env bash
# .git/hooks/pre-commit (chmod +x)
staged=$(git diff --cached --name-only --diff-filter=ACM)
[ -z "$staged" ] && exit 0
payload=$(echo "$staged" | xargs cat)
analysis=$(echo "$payload" | \
claude -p "Review these changes for issues before commit" \
--allowedTools "View" \
--output-format json)
if echo "$analysis" | jq -e '.critical_issues[]' >/dev/null 2>&1; then
echo "❌ Critical issues found – commit blocked"
exit 1
fi
echo "✅ Claude analysis passed"
/init
生成CLAUDE.md
,让Claude记住你的项目Project: My Application
Overview
This is a React/Node.js application with PostgreSQL database.
Architecture
Frontend: React 18 with TypeScript
Backend: Node.js with Express
Database: PostgreSQL 14
Current Goals
Implement authentication
Add API documentation
Set up CI/CD pipeline
Development Guidelines
Use TypeScript for all new code
Follow ESLint configuration
Write tests for new features
claude /memory
claude /memory view
claude --add-dir ../frontend ../backend ../shared
claude "analyze the entire application architecture"
这份指南细到你想不到,命令全给你写明白,坑全帮你踩过。工具嘛,关键是用得顺手,别让它反过来折腾你。你们是怎么高效使用Claude Code的?评论区分享讨论吧!
国内镜像链接
https://aicodewith.com/?invitation=EK1S5F