终极 Claude 代码技巧合集 (Advent of Claude 2025)

发布: (2026年1月3日 GMT+8 09:47)
10 分钟阅读
原文: Dev.to

Source: Dev.to

完整出处:本文汇总了 Ado 的精彩 Advent of Claude 系列,其中他在 2025 年 12 月每日分享 Claude Code 小技巧。Ado 是 Anthropic 的开发者关系专业人士。所有技巧均来源于他的 Twitter/X 发帖——我只是将它们整理成一本参考指南。

注意: 在 2025 年 12 月期间,Ado (@adocomplete) 在他的 Advent of Claude 系列中每日发布 Claude Code 小技巧。本文将全部 31 条技巧汇总为一份完整的参考指南。

! 前缀 – 即时 Bash 执行

# Instead of: "Can you run git status?"
!git status

# Or check test results directly:
!npm test

# View logs:
!tail -50 app.log

为什么重要: 无需模型处理,无延迟,无多余 token。输出直接进入上下文。

使用 Esc Esc 进行倒带

Esc Esc

您可以倒带:

  • 仅对话
  • 仅代码
  • 两者皆可

自由尝试实验性方法——随时都有逃生口可用。

为 API 使用启用 “Thinking”

const response = await client.messages.create({
  model: "claude-sonnet-4-20250514",
  max_tokens: 8000,
  thinking: {
    type: "enabled",
    budget_tokens: 5000
  },
  messages: [{ role: "user", content: "…" }]
});

Claude 在响应之前会在 thinking 块中显示逐步推理。

claude --continue / --resume – 永不丢失你的工作

# Pick up your last conversation instantly
claude --continue

# Choose from past sessions with a picker
claude --resume

上下文会被保留,对话保持完整,你可以从离开的地方继续。

# Prefix – Save to Memory

# Always use pnpm instead of npm in this project
# The API endpoint is https://api.example.com/v2
# Run tests with: pytest -v --cov

Claude 将会询问存放位置(例如 CLAUDE.md、用户设置等)。记忆会被更新,并在后续提示中保持可用。

/vim – 用于提示撰写的 Vim 模式

/vim

键绑定

操作
h j k l导航
ciw更改内部单词
dd删除行
w b单词导航
yy p复制并粘贴

停止使用鼠标。

Source:

-p 前缀 – 在脚本、CI/CD 或自动化中运行 Claude 代码

# Direct prompt execution
claude -p "Fix the lint errors"

# Pipe input for context
git diff | claude -p "Explain these changes"

# Use in CI/CD
cat error.log | claude -p "Diagnose this error and suggest fixes"

/context – 可视化令牌使用情况

/context

显示以下细分:

  • 系统提示大小
  • MCP 服务器开销
  • 已加载的内存文件
  • 对话历史
  • 剩余可用空间

专业提示: MCP 工具仅因可用就可能消耗 8‑30 % 的上下文。请移除未使用的工具。

& 前缀 – 发送到云

# Send a task to run in the cloud
& Refactor the authentication module

# Later, pull it back locally
claude --teleport session_abc123

工作在本地继续进行,而 Claude 在远程运行——两全其美。

/stats – 使用分析

/stats

显示您使用情况的活动图表。 “橙色是新的绿色”。

为便捷检索命名会话

# Name your current session
/rename api-migration

# Resume by name later
/resume api-migration

或者在命令行中:

claude --resume api-migration

你的分支都有名称;你的会话也应该如此。

魔法思考词

关键字思考代币
think4 000
think hard10 000
ultrathink31 999

示例

ultrathink - Review this architecture and identify potential scaling issues

思考越多 → 对复杂问题的分析越彻底。

Prompt Stash – Like git stash

Ctrl+S  → Saves your current draft
          → Send something else
          → Draft auto‑restores

不再需要复制到草稿本。再也不会失去思路。

/statusline – 自定义状态栏

/statusline

Configure via settings to display:

  • Git 分支和状态
  • 当前模型
  • 令牌计数
  • 自定义指标(天气、比特币价格,任何内容)

跳过权限提示

claude --dangerously-skip-permissions

自动批准所有操作。请谨慎使用 – 非常适合可信赖的、重复性的工作流。

生命周期钩子

/hooks

或在 .claude/settings.json 中:

{
  "hooks": {
    "PreToolUse": ["./scripts/check-secrets.sh"],
    "PostToolUse": ["./scripts/format-code.sh"],
    "UserPromptSubmit": ["./scripts/validate-prompt.sh"]
  }
}

使用场景

  • 阻止危险命令(rm -rf
  • 编辑后自动格式化
  • 从提示中过滤敏感数据
  • 发送通知

@ 提及 – 按需上下文

# Add files to context
@src/api/auth.ts
@package.json

# List directory contents
@src/components/

# Manage MCP servers
@mcp-server enable
@my-subagent invoke

即时添加一个或多个文件,列出目录,启用/禁用 MCP 服务器,并调用子代理。

预测性后续

  • 完成任务 → 会出现灰色的后续建议。
  • Tab → 接受并编辑建议。
  • Enter → 接受并立即运行。

正如 Tab 自动补全代码,它现在也会自动补全你的工作流程。

/chrome – 浏览器集成

/chrome

让 Claude 直接与您的浏览器交互,启用以下任务:

  • 浏览页面
  • 提取数据
  • 执行点击和表单填写

(为简洁起见,省略了进一步细节。)

Source:

Claude Code 速查表

核心能力

  • 导航到 URL
  • 点击按钮和链接
  • 读取控制台错误
  • 检查 DOM
  • 填写表单
  • 截取屏幕截图
  • “修复 bug 并验证其有效” → 一次提示完成

技能(文件夹结构)

.claude/skills/
├── api-design/
│   ├── SKILL.md
│   └── templates/
├── testing/
│   ├── SKILL.md
│   └── examples/
  • 打包一次 → 可在任何地方使用。
  • 代理技能现已成为开放标准。

最佳实践

  • 保持 SKILL.md ≤ 500 行
  • 包含 具体示例
  • 使用 渐进式披露
  • 跨模型测试

插件与设置

/plugin install my-setup

插件包

  • 命令
  • 代理
  • 技能
  • 钩子
  • MCP 配置

通过市场发现新工作流。

提示历史搜索

快捷键操作
Ctrl + R开始搜索
Ctrl + R(再次)循环浏览匹配项
Enter运行选中的提示
Tab编辑第一个结果

无需重新输入 – 通过 /init 召回(自动生成文档)。

自动生成的入职文档

/init

创建一个 CLAUDE.md,内容包括:

  • 构建命令
  • 测试说明
  • 关键目录
  • 项目约定
  • 常用工作流

每个人都需要入职文档 – Claude 为你生成。

导出对话

/export
  • 将整个对话导出为 Markdown

    • 你发送的每个提示
    • 收到的每个回复
    • 所有工具调用
  • 适用于文档、培训或工作证明。

并行子代理

  • 每个子代理拥有:

    • 独立的 200 k 上下文窗口
    • 独立执行
    • 结果合并回主对话
  • 适用于:

    • 并行代码探索
    • 并发审查操作
    • 多文件分析

可复用的斜杠命令

.claude/commands/ 中创建 markdown 文件:

---
description: Review code for best practices
arguments:
  - name: file
    description: File to review
---

Review {{file}} for:
1. Security vulnerabilities
2. Performance issues
3. Code style

使用方式:

/review src/api/auth.ts

沙盒模式 – 受控自由

/sandbox
  • 一次定义边界。
  • Claude 在边界内自由工作。
  • “YOLO 速度”并兼顾真实安全。

使用情况监控

/usage

显示当前使用量与计划限制 – 防止意外超额。

计划模式(只读)

Shift+Tab, Shift+Tab   →   Plan mode

在计划模式下 Claude 可以:

  • 读取文件
  • 搜索代码
  • 分析模式
  • 探索代码库

不会编辑 任何内容。

“三思而后行。” – Ado

LSP 集成

Claude Code 现已集成 语言服务器协议 (Language Server Protocol)

  • 即时诊断 – 错误和警告实时呈现
  • 导航 – 跳转到定义、查找引用等

Claude 像 IDE 一样理解你的代码。

代理 SDK

为 Claude Code 提供的相同代理循环、工具和上下文管理,现在作为 SDK 可用。

只需约 10 行代码,即可构建与 Claude Code 行为相同的代理。

功能时间线

功能命令 / 快捷键
1Instant Bash!command
2RewindEsc Esc
3Extended ThinkingAPI config
4Resume Session--continue / --resume
5Save to Memory#message
6Vim Mode/vim
7Headless Mode-p "prompt"
8Context View/context
9Cloud Offload&prompt / --teleport
10Usage Stats/stats
11Named Sessions/rename / /resume
12Deep Thinkingultrathink
13Stash PromptCtrl+S
14Status Bar/statusline
15Skip Permissions--dangerously-skip-permissions
16Lifecycle Hooks/hooks
17Add Context@file / @dir/
18Prompt SuggestionsTab / Enter
19Browser Control/chrome
20Agent Skills.claude/skills/
21Plugins/plugin install
22History SearchCtrl+R
23Auto Docs/init
24Export Chat/export
25Parallel AgentsSubagents
26Custom Commands.claude/commands/
27Sandbox Mode/sandbox
28Check Limits/usage
29Plan ModeShift+Tab twice
30LSP IntegrationLanguage Server Protocol
31Agent SDKSDK

致谢

衷心感谢 Ado 创作并分享 Advent of Claude 系列。这些技巧提升了开发者使用 Claude Code 的水平。

  • 在 X 上关注 Ado:@adocomplete,获取最新技巧和更新。

本文是对 Ado 原始 Advent of Claude 系列的社区总结。所有技巧的归功于 Ado 以及 Anthropic 的 Claude Code 团队。

Back to Blog

相关文章

阅读更多 »

RGB LED 支线任务 💡

markdown !Jennifer Davishttps://media2.dev.to/dynamic/image/width=50,height=50,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%...

Mendex:我为何构建

介绍 大家好。今天我想分享一下我是谁、我在构建什么以及为什么。 早期职业生涯与倦怠 我在 17 年前开始我的 developer 生涯……