技能:更新 Claude Code 配置
Skill: Update Claude Code Config
v2.1.9Skill for modifying Claude Code configuration file (settings.json).
模板变量 / Template Variables
SETTINGS_FILE_LOCATION_PROMPTHOOKS_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,modellanguage,alwaysThinkingEnabledpermissions.defaultMode
直接编辑 settings.json 用于:
- Hooks (PreToolUse, PostToolUse 等)
- 复杂的权限规则 (allow/deny 数组)
- 环境变量
- MCP 服务器配置
- 插件配置
工作流程
- 澄清意图 - 如果请求不明确,进行询问
- 读取现有文件 - 使用 Read 工具读取目标 settings 文件
- 小心合并 - 保留现有设置,特别是数组
- 编辑文件 - 使用 Edit 工具(如果文件不存在,请用户先创建它)
- 确认 - 告诉用户更改了什么
合并数组(重要!)
当向权限数组或 hook 数组添加内容时,与现有数组合并,不要替换:
错误(替换了现有权限):
{ "permissions": { "allow": ["Bash(npm:*)"] } }正确(保留现有 + 添加新的):
{
"permissions": {
"allow": [
"Bash(git:*)", // 现有的
"Edit(.claude)", // 现有的
"Bash(npm:*)" // 新的
]
}
}${SETTINGS_FILE_LOCATION_PROMPT}
${HOOKS_CONFIGURATION_PROMPT}
示例工作流程
添加 Hook
用户:"在 Claude 写入代码后格式化我的代码"
- 澄清:使用哪个格式化工具?(prettier, gofmt 等)
- 读取:
.claude/settings.json(如果缺失则创建) - 合并:添加到现有 hooks,不要替换
- 结果:
{
"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 命令而无需提示"
- 读取:现有权限
- 合并:将
Bash(npm:*)添加到 allow 数组 - 结果:与现有的允许项合并
环境变量
用户:"设置 DEBUG=true"
- 决定:用户设置(全局)还是项目设置?
- 读取:目标文件
- 合并:添加到 env 对象
{ "env": { "DEBUG": "true" } }需要避免的常见错误
- 替换而非合并 - 始终保留现有设置
- 选错文件 - 如果范围不明确,询问用户
- 无效的 JSON - 更改后验证语法
- 忘记先读取 - 始终先读后写
Hook 故障排除
如果 hook 没有运行:
- 检查 settings 文件 - 读取 ~/.claude/settings.json 或 .claude/settings.json
- 验证 JSON 语法 - 无效的 JSON 会静默失败
- 检查匹配器 - 它是否匹配工具名称?(例如 "Bash", "Write", "Edit")
- 检查 hook 类型 - 是 "command", "prompt", 还是 "agent"?
- 测试命令 - 手动运行 hook 命令以查看是否有效
- 使用 --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,modellanguage,alwaysThinkingEnabledpermissions.defaultMode
Edit settings.json directly for:
- Hooks (PreToolUse, PostToolUse, etc.)
- Complex permission rules (allow/deny arrays)
- Environment variables
- MCP server configuration
- Plugin configuration
Workflow
- Clarify intent - Ask if the request is ambiguous
- Read existing file - Use Read tool on the target settings file
- Merge carefully - Preserve existing settings, especially arrays
- Edit file - Use Edit tool (if file doesn't exist, ask user to create it first)
- 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):
{ "permissions": { "allow": ["Bash(npm:*)"] } }RIGHT (preserves existing + adds new):
{
"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"
- Clarify: Which formatter? (prettier, gofmt, etc.)
- Read:
.claude/settings.json(or create if missing) - Merge: Add to existing hooks, don't replace
- Result:
{
"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"
- Read: Existing permissions
- Merge: Add
Bash(npm:*)to allow array - Result: Combined with existing allows
Environment Variables
User: "Set DEBUG=true"
- Decide: User settings (global) or project settings?
- Read: Target file
- Merge: Add to env object
{ "env": { "DEBUG": "true" } }Common Mistakes to Avoid
- Replacing instead of merging - Always preserve existing settings
- Wrong file - Ask user if scope is unclear
- Invalid JSON - Validate syntax after changes
- Forgetting to read first - Always read before write
Troubleshooting Hooks
If a hook isn't running:
- Check the settings file - Read ~/.claude/settings.json or .claude/settings.json
- Verify JSON syntax - Invalid JSON silently fails
- Check the matcher - Does it match the tool name? (e.g., "Bash", "Write", "Edit")
- Check hook type - Is it "command", "prompt", or "agent"?
- Test the command - Run the hook command manually to see if it works
- Use --debug - Run
claude --debugto see hook execution logs