Skip to content

0.7 全局配置 — 让 Claude Code 按你的习惯工作

学习目标

  • 知道 Claude Code 的配置文件在哪
  • 复制一份推荐配置直接用
  • 理解每个配置项的作用

配置文件在哪?

~/.claude/settings.json
  • macOS / Linux/Users/你的用户名/.claude/settings.json
  • WindowsC:\Users\你的用户名\.claude\settings.json

如果不存在,手动创建:

bash
mkdir -p ~/.claude
nano ~/.claude/settings.json

推荐配置(直接复制)

下面是一份经过实战验证的配置,直接复制进去就能用

json
{
  "$schema": "https://json.schemastore.org/claude-code-settings.json",
  "language": "chinese",
  "outputStyle": "Concise",
  "effortLevel": "high",
  "alwaysThinkingEnabled": true,
  "respectGitignore": false,
  "includeGitInstructions": false,
  "feedbackSurveyRate": 0,
  "spinnerTipsEnabled": true,
  "voiceEnabled": true,
  "autoDreamEnabled": true,
  "autoUpdatesChannel": "stable",
  "cleanupPeriodDays": 14,
  "permissions": {
    "allow": [
      "Read",
      "Edit",
      "Write",
      "Glob",
      "Grep",
      "WebFetch",
      "WebSearch",
      "Agent",
      "Bash(npm *)",
      "Bash(npx *)",
      "Bash(node *)",
      "Bash(bun *)",
      "Bash(pnpm *)",
      "Bash(git *)",
      "Bash(python3 *)",
      "Bash(curl *)",
      "Bash(ls *)",
      "Bash(mkdir *)",
      "Bash(rm *)",
      "Bash(cp *)",
      "Bash(mv *)",
      "Bash(cat *)",
      "Bash(head *)",
      "Bash(tail *)"
    ],
    "deny": [
      "Read(./.env)",
      "Read(./.env.*)",
      "Read(./secrets/**)",
      "Read(./**/credentials*)"
    ]
  },
  "env": {
    "CLAUDE_CODE_MAX_OUTPUT_TOKENS": "16000",
    "MAX_CONSECUTIVE_AUTOCOMPACT_FAILURES": "3",
    "CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS": "1"
  },
  "attribution": {
    "commit": "",
    "pr": ""
  }
}

每个配置项的作用

基础设置

配置项作用
language"chinese"界面和回复用中文
outputStyle"Concise"回复简洁不废话(也可以选 "Verbose" 详细模式)
effortLevel"high"推理深度设为高,回答质量更好
alwaysThinkingEnabledtrue默认开启扩展思考,复杂问题想得更深
respectGitignorefalse不受 .gitignore 限制,所有文件都能看到
includeGitInstructionsfalse不自动加 git 上下文(减少 token 消耗)
feedbackSurveyRate0关闭反馈调查弹窗
voiceEnabledtrue开启语音输入功能
autoDreamEnabledtrue开启后台自动任务
cleanupPeriodDays14临时文件保留 14 天

permissions — 权限控制

allow 列表里的操作会自动执行,不需要每次弹窗确认:

权限含义
Read / Edit / Write读/改/创建文件
Glob / Grep搜索文件名/文件内容
WebFetch / WebSearch访问网页/搜索
Agent派出子智能体
Bash(git *)执行 git 命令
Bash(npm *) / Bash(pnpm *)执行包管理器命令
Bash(node *) / Bash(python3 *)运行脚本
Bash(curl *)下载/请求
Bash(ls/mkdir/rm/cp/mv/cat/head/tail *)常用文件操作命令

deny 列表里的操作会被禁止,即使你手动确认也不行:

json
"deny": [
  "Read(./.env)",         // 禁止读取环境变量文件(里面有密钥)
  "Read(./.env.*)",       // 禁止读取所有 .env 变体
  "Read(./secrets/**)",   // 禁止读取 secrets 目录
  "Read(./**/credentials*)"  // 禁止读取任何 credentials 文件
]

这样即使 Claude Code 想看你的密钥,也看不了。安全第一。

env — 环境变量

变量作用
CLAUDE_CODE_MAX_OUTPUT_TOKENS单次回复最大长度,设大一点避免回复被截断
MAX_CONSECUTIVE_AUTOCOMPACT_FAILURES自动压缩最大失败次数
CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS设为 "1" 开启团队模式,可以让多个 Claude Code 并行协作(进阶篇会详细讲)

attribution — Git 署名

json
"attribution": {
  "commit": "",
  "pr": ""
}

设为空字符串 = Claude Code 帮你提交代码时不加任何"AI 生成"的署名。如果你想加署名,可以写:

json
"commit": "Co-Authored-By: Claude <claude@anthropic.com>"

配置文件层级

Claude Code 有多层配置,优先级从高到低:

层级路径说明
项目本地项目/.claude/settings.local.json只影响你自己,不提交 git
项目团队项目/.claude/settings.json团队共享,提交到 git
全局用户~/.claude/settings.json你的所有项目通用

一般只用全局配置就够了。


小结

  • 配置文件在 ~/.claude/settings.json
  • 上面的推荐配置直接复制就能用
  • 核心是 permissions(权限)— 控制哪些操作自动执行、哪些禁止
  • deny 里一定要加 .envcredentials,保护你的密钥
  • 配置改完退出 Claude Code 重新进入生效

下一节 → 和 Claude Code 对话的正确姿势

用 Claude Code 构建,用 VitePress 驱动