Claude Code VS Code 集成指南
专为不同平台用户设计的 VS Code 集成详细教程。
🎯 概述
Claude Code 提供了强大的 VS Code 集成功能,让你可以在熟悉的编辑器环境中享受 AI 辅助编程的便利。
📋 前置要求
⚠️
确保已正确安装 Claude Code CLI 工具和配置环境变量。如果尚未安装,请查看:
📝 必需配置(v2.0.0+)
🚨
重要:VS Code 插件 2.0.0+ 版本必须配置此文件,否则无法使用!
配置步骤
macOS / Linux
配置文件路径:~/.claude/config.json
# 创建配置目录和文件
mkdir -p ~/.claude
nano ~/.claude/config.json
将以下内容粘贴到文件中:
{
"primaryApiKey": "aihezu"
}
保存文件(Ctrl+O
然后 Enter
,Ctrl+X
退出)。
Windows
配置文件路径:C:\Users\你的用户名\.claude\config.json
- 打开文件资源管理器
- 导航到
C:\Users\你的用户名\
(将「你的用户名」替换为实际的 Windows 用户名) - 创建
.claude
文件夹(如果不存在) - 在
.claude
文件夹中创建config.json
文件 - 使用记事本打开并输入以下内容:
{
"primaryApiKey": "aihezu"
}
保存文件。
验证配置
ℹ️
配置完成后,请重启 VS Code,确保配置生效。
🍎 macOS 集成
安装 VS Code 扩展
方法一:自动安装(推荐)
# 在 VS Code 终端中运行 claude 会自动安装扩展
# 在 VS Code 中打开项目目录,然后在终端运行:
claude
方法二:手动安装
- 打开 VS Code
- 按
Cmd+Shift+X
打开扩展市场 - 搜索 "Claude Code"
- 点击安装
配置 VS Code
在 VS Code 中使用快捷键:
Cmd+Shift+P
打开命令面板- 输入 "Claude: Start Chat" 开始对话
macOS 特定设置
# 确保 VS Code 可以访问环境变量
echo 'export ANTHROPIC_BASE_URL="https://cc.aihezu.dev/api"' >> ~/.zshrc
echo 'export ANTHROPIC_AUTH_TOKEN="你的API密钥"' >> ~/.zshrc
source ~/.zshrc
🪟 Windows 集成
安装 VS Code 扩展
方法一:自动安装(推荐)
# 在 VS Code 终端中运行 claude 会自动安装扩展
# 在 VS Code 中打开项目目录,然后在终端运行:
claude
方法二:手动安装
- 打开 VS Code
- 按
Ctrl+Shift+X
打开扩展市场 - 搜索 "Claude Code"
- 点击安装
配置 VS Code
在 VS Code 中使用快捷键:
Ctrl+Shift+P
打开命令面板- 输入 "Claude: Start Chat" 开始对话
Windows 特定设置
确保环境变量在 VS Code 中生效:
# 检查 VS Code 是否能访问环境变量
echo $env:ANTHROPIC_BASE_URL
echo $env:ANTHROPIC_AUTH_TOKEN
ℹ️
如果环境变量在 VS Code 中不生效,请重启 VS Code 或重启电脑。
🐧 Linux 集成
安装 VS Code(如果未安装)
Ubuntu/Debian
# 下载并安装 VS Code
wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > packages.microsoft.gpg
sudo install -o root -g root -m 644 packages.microsoft.gpg /etc/apt/trusted.gpg.d/
sudo sh -c 'echo "deb [arch=amd64,arm64,armhf signed-by=/etc/apt/trusted.gpg.d/packages.microsoft.gpg] https://packages.microsoft.com/repos/code stable main" > /etc/apt/sources.list.d/vscode.list'
sudo apt update
sudo apt install code
Arch Linux
# 使用 AUR
yay -S visual-studio-code-bin
安装 Claude Code 扩展
方法一:自动安装(推荐)
# 在 VS Code 终端中运行 claude 会自动安装扩展
# 在 VS Code 中打开项目目录,然后在终端运行:
claude
方法二:手动安装
- 打开 VS Code
- 按
Ctrl+Shift+X
打开扩展市场 - 搜索 "Claude Code"
- 点击安装
配置 VS Code
在 VS Code 中使用快捷键:
Ctrl+Shift+P
打开命令面板- 输入 "Claude: Start Chat" 开始对话
WSL2 特殊配置
如果在 WSL2 环境中使用:
# 安装 WSL2 扩展
code --install-extension ms-vscode-remote.remote-wsl
# 在 WSL2 中打开项目
code .
确保环境变量在 WSL2 中正确设置:
# 确保 WSL2 中的环境变量正确
env | grep ANTHROPIC
⚙️ 高级配置
VS Code 设置
创建 VS Code 工作区设置:
// .vscode/settings.json
{
"claude.enabled": true,
"claude.autoSave": true,
"claude.maxTokens": 4096,
"claude.suggestions.enabled": true,
"claude.suggestions.maxSuggestions": 5
}
快捷键自定义
在 VS Code 中自定义快捷键(Ctrl/Cmd+Shift+P
→ "Preferences: Open Keyboard Shortcuts"):
// keybindings.json
[
{
"key": "ctrl+alt+c",
"command": "claude.startChat",
"when": "editorTextFocus"
},
{
"key": "ctrl+alt+s",
"command": "claude.stopChat"
}
]
项目级配置
在项目根目录创建 .claude/vscode.json
:
{
"theme": "auto",
"autoSave": true,
"suggestions": {
"enabled": true,
"triggerOnType": true
},
"integrations": {
"terminal": true,
"git": true,
"debugger": true
}
}
🚀 使用示例
启动对话
- 打开 VS Code
- 打开你的项目文件夹
- 按
Ctrl/Cmd+Shift+P
- 输入 "Claude: Start Chat"
- 开始与 Claude 对话
常用命令
Claude: Start Chat
- 开始新对话Claude: Stop Chat
- 停止当前对话Claude: Clear History
- 清除对话历史Claude: Show Settings
- 显示设置面板Claude: Reload Extension
- 重新加载扩展
代码协作
# 示例:在 Python 文件中与 Claude 协作
def fibonacci(n):
# 选中这个函数,然后使用 Claude: Explain Code
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
🔧 常见 VS Code 集成问题
扩展无法连接
- 重启 VS Code
- 检查环境变量设置
- 确认 Claude Code 可在终端正常使用
命令面板中找不到 Claude 命令
- 确认扩展已正确安装
- 重新加载扩展
- 检查扩展是否启用
WSL2 中的连接问题
- 确保在 WSL2 中安装了 Remote-WSL 扩展
- 检查 WSL2 中的环境变量
- 确认 Windows 防火墙设置
📱 获取支持
VS Code 集成遇到问题?我们提供专业技术支持:
🎉
恭喜! 你已成功配置 Claude Code VS Code 集成,现在可以在编辑器中享受 AI 辅助编程了!