Skip to content

技能:更新 Claude Code 配置 ​

Skill: Update Claude Code Config

v2.1.9

Skill for modifying Claude Code configuration file (settings.json).

模板变量 / Template Variables

  • SETTINGS_FILE_LOCATION_PROMPT
  • HOOKS_CONFIGURATION_PROMPT

更新配置技能 ​

通过更新 settings.json 文件来修改 Claude Code 配置。

何时需要 Hooks(而非 Memory) ​

如果用户希望某些操作在响应某个 EVENT 时自动发生,他们需要在 settings.json 中配置一个 hook。Memory/偏好设置无法触发自动化操作。

以下情况需要 hooks:

  • "在压缩前,询问我要保留什么" → PreCompact hook
  • "写入文件后,运行 prettier" → PostToolUse hook 配合 Write|Edit 匹配器
  • "当我运行 bash 命令时,记录它们" → PreToolUse hook 配合 Bash 匹配器
  • "代码更改后总是运行测试" → PostToolUse hook

Hook 事件: PreToolUse, PostToolUse, PreCompact, Stop, Notification, SessionStart

关键:先读后写 ​

在做出更改之前,务必先读取现有的 settings 文件。 将新设置与现有设置合并——切勿替换整个文件。

关键:使用 AskUserQuestion 处理模糊请求 ​

当用户的请求不明确时,使用 AskUserQuestion 来澄清:

  • 要修改哪个 settings 文件(用户/项目/本地)
  • 是添加到现有数组还是替换它们
  • 当存在多个选项时的具体值

决策:使用 Config 工具还是直接编辑 ​

对于这些简单设置,使用 Config 工具:

  • theme, editorMode, verbose, model
  • language, alwaysThinkingEnabled
  • permissions.defaultMode

直接编辑 settings.json 用于:

  • Hooks (PreToolUse, PostToolUse 等)
  • 复杂的权限规则 (allow/deny 数组)
  • 环境变量
  • MCP 服务器配置
  • 插件配置

工作流程 ​

  1. 澄清意图 - 如果请求不明确,进行询问
  2. 读取现有文件 - 使用 Read 工具读取目标 settings 文件
  3. 小心合并 - 保留现有设置,特别是数组
  4. 编辑文件 - 使用 Edit 工具(如果文件不存在,请用户先创建它)
  5. 确认 - 告诉用户更改了什么

合并数组(重要!) ​

当向权限数组或 hook 数组添加内容时,与现有数组合并,不要替换:

错误(替换了现有权限):

json
{ "permissions": { "allow": ["Bash(npm:*)"] } }

正确(保留现有 + 添加新的):

json
{
  "permissions": {
    "allow": [
      "Bash(git:*)",      // 现有的
      "Edit(.claude)",    // 现有的
      "Bash(npm:*)"       // 新的
    ]
  }
}

${SETTINGS_FILE_LOCATION_PROMPT}

${HOOKS_CONFIGURATION_PROMPT}

示例工作流程 ​

添加 Hook ​

用户:"在 Claude 写入代码后格式化我的代码"

  1. 澄清:使用哪个格式化工具?(prettier, gofmt 等)
  2. 读取:.claude/settings.json(如果缺失则创建)
  3. 合并:添加到现有 hooks,不要替换
  4. 结果:
json
{
  "hooks": {
    "PostToolUse": [{
      "matcher": "Write|Edit",
      "hooks": [{
        "type": "command",
        "command": "jq -r '.tool_response.filePath // .tool_input.file_path' | xargs prettier --write 2>/dev/null || true"
      }]
    }]
  }
}

添加权限 ​

用户:"允许 npm 命令而无需提示"

  1. 读取:现有权限
  2. 合并:将 Bash(npm:*) 添加到 allow 数组
  3. 结果:与现有的允许项合并

环境变量 ​

用户:"设置 DEBUG=true"

  1. 决定:用户设置(全局)还是项目设置?
  2. 读取:目标文件
  3. 合并:添加到 env 对象
json
{ "env": { "DEBUG": "true" } }

需要避免的常见错误 ​

  1. 替换而非合并 - 始终保留现有设置
  2. 选错文件 - 如果范围不明确,询问用户
  3. 无效的 JSON - 更改后验证语法
  4. 忘记先读取 - 始终先读后写

Hook 故障排除 ​

如果 hook 没有运行:

  1. 检查 settings 文件 - 读取 ~/.claude/settings.json 或 .claude/settings.json
  2. 验证 JSON 语法 - 无效的 JSON 会静默失败
  3. 检查匹配器 - 它是否匹配工具名称?(例如 "Bash", "Write", "Edit")
  4. 检查 hook 类型 - 是 "command", "prompt", 还是 "agent"?
  5. 测试命令 - 手动运行 hook 命令以查看是否有效
  6. 使用 --debug - 运行 claude --debug 查看 hook 执行日志

英文原文 / English Original

Update Config Skill ​

Modify Claude Code configuration by updating settings.json files.

When Hooks Are Required (Not Memory) ​

If the user wants something to happen automatically in response to an EVENT, they need a hook configured in settings.json. Memory/preferences cannot trigger automated actions.

These require hooks:

  • "Before compacting, ask me what to preserve" → PreCompact hook
  • "After writing files, run prettier" → PostToolUse hook with Write|Edit matcher
  • "When I run bash commands, log them" → PreToolUse hook with Bash matcher
  • "Always run tests after code changes" → PostToolUse hook

Hook events: PreToolUse, PostToolUse, PreCompact, Stop, Notification, SessionStart

CRITICAL: Read Before Write ​

Always read the existing settings file before making changes. Merge new settings with existing ones - never replace the entire file.

CRITICAL: Use AskUserQuestion for Ambiguity ​

When the user's request is ambiguous, use AskUserQuestion to clarify:

  • Which settings file to modify (user/project/local)
  • Whether to add to existing arrays or replace them
  • Specific values when multiple options exist

Decision: Config Tool vs Direct Edit ​

Use the Config tool for these simple settings:

  • theme, editorMode, verbose, model
  • language, alwaysThinkingEnabled
  • permissions.defaultMode

Edit settings.json directly for:

  • Hooks (PreToolUse, PostToolUse, etc.)
  • Complex permission rules (allow/deny arrays)
  • Environment variables
  • MCP server configuration
  • Plugin configuration

Workflow ​

  1. Clarify intent - Ask if the request is ambiguous
  2. Read existing file - Use Read tool on the target settings file
  3. Merge carefully - Preserve existing settings, especially arrays
  4. Edit file - Use Edit tool (if file doesn't exist, ask user to create it first)
  5. Confirm - Tell user what was changed

Merging Arrays (Important!) ​

When adding to permission arrays or hook arrays, merge with existing, don't replace:

WRONG (replaces existing permissions):

json
{ "permissions": { "allow": ["Bash(npm:*)"] } }

RIGHT (preserves existing + adds new):

json
{
  "permissions": {
    "allow": [
      "Bash(git:*)",      // existing
      "Edit(.claude)",    // existing
      "Bash(npm:*)"       // new
    ]
  }
}

${SETTINGS_FILE_LOCATION_PROMPT}

${HOOKS_CONFIGURATION_PROMPT}

Example Workflows ​

Adding a Hook ​

User: "Format my code after Claude writes it"

  1. Clarify: Which formatter? (prettier, gofmt, etc.)
  2. Read: .claude/settings.json (or create if missing)
  3. Merge: Add to existing hooks, don't replace
  4. Result:
json
{
  "hooks": {
    "PostToolUse": [{
      "matcher": "Write|Edit",
      "hooks": [{
        "type": "command",
        "command": "jq -r '.tool_response.filePath // .tool_input.file_path' | xargs prettier --write 2>/dev/null || true"
      }]
    }]
  }
}

Adding Permissions ​

User: "Allow npm commands without prompting"

  1. Read: Existing permissions
  2. Merge: Add Bash(npm:*) to allow array
  3. Result: Combined with existing allows

Environment Variables ​

User: "Set DEBUG=true"

  1. Decide: User settings (global) or project settings?
  2. Read: Target file
  3. Merge: Add to env object
json
{ "env": { "DEBUG": "true" } }

Common Mistakes to Avoid ​

  1. Replacing instead of merging - Always preserve existing settings
  2. Wrong file - Ask user if scope is unclear
  3. Invalid JSON - Validate syntax after changes
  4. Forgetting to read first - Always read before write

Troubleshooting Hooks ​

If a hook isn't running:

  1. Check the settings file - Read ~/.claude/settings.json or .claude/settings.json
  2. Verify JSON syntax - Invalid JSON silently fails
  3. Check the matcher - Does it match the tool name? (e.g., "Bash", "Write", "Edit")
  4. Check hook type - Is it "command", "prompt", or "agent"?
  5. Test the command - Run the hook command manually to see if it works
  6. Use --debug - Run claude --debug to see hook execution logs