กลับไปหน้าสูตร
#claude#anthropic#cli#cheatsheet#ai#sonnet
Claude Code Cheatsheet
คู่มือฉบับเต็ม Claude Code CLI: เจาะลึกการใช้ Sonnet 3.5/4.5 พัฒนาโปรเจกต์, การจัดการ Permission และ MCP Integration
11 มีนาคม 2569อ่านประมาณ 5 นาที
สารบัญสูตร
🏗 Core CLI Commands (จาก Terminal)🛠 Interactive Slash Commands🚩 Useful Flags (ตอนรันคำสั่ง)🧾 Commands จาก Official CLI Reference👥 Agent Team & Remote Workflow📦 Official Install & Auth Notes🌟 จุดเด่นเฉพาะตัวของ Claude Code🖱 Keyboard Shortcuts (Interactive Mode)📄 Configuration Files🤖 Subagents & Agent Teams🧩 Skills & Custom Commands⛓ Model Context Protocol (MCP)⚡ Automation Recipes (Examples)📅 Daily Pro Workflow (Team)
🏗 Core CLI Commands (จาก Terminal)
| Command | การใช้งานเจาะลึก |
|---|---|
claude | เริ่มต้นเซสชันใหม่ใน directory ปัจจุบัน |
claude "prompt" | เริ่มต้นเซสชันพร้อมส่งคำสั่งทันที |
claude -p "query" | Print Mode: รันคำสั่งแบบครั้งเดียวจบ (Non-interactive) |
claude -c | สานต่อการสนทนาล่าสุด (Continue) ในโปรเจกต์เดิม |
claude -r <id> | โหลดเซสชันที่ระบุ ID กลับมาคุยต่อ |
claude update | ตรวจสอบและอัปเดตเวอร์ชันผ่าน terminal |
claude auth login | ล็อกอินผ่านเว็บเบราว์เซอร์ |
claude auth status | แสดงข้อมูล Token, Account และสถานะการล็อกอิน |
🛠 Interactive Slash Commands
📁 Project Intelligence
| Command | การใช้งาน |
|---|---|
/init | สแกนหาไฟล์โปรเจกต์และสร้าง CLAUDE.md เพื่อเก็บ Best Practices |
/status | เช็กสถานะเซสชัน: โมเดลที่ใช้, ไฟล์ที่ Claude กำลังเฝ้าดู |
/clear | ล้าง Context ประวัติการคุย เพื่อประหยัด Token และเพิ่มความเร็ว |
/compact | ย่อประวัติการคุยยาวๆ ให้กะทัดรัด (Summary) |
/cost | แสดงยอด Token consumed และราคาที่ใช้ไปในเซสชันปัจจุบัน |
⚙️ Settings & Control
| Command | การใช้งาน |
|---|---|
/model | สลับโมเดลระหว่าง sonnet-3-5, sonnet-4-5, opus-4-5 |
/permissions | ตั้งค่าการอนุญาต เช่น "Allow All", "Confirm Before Write" |
/mcp | จัดการการเชื่อมต่อกับ Model Context Protocol servers |
/memory | ดูและจัดการ Persistent Memory (ความจำส่วนตัวที่ AI จดจำไว้) |
/terminal-setup | ตั้งค่า Terminal เพื่อรองรับ Multiline และ Keybindings |
/vim | เปิด/ปิดการใช้งาน Vim-style keybindings สำหรับ Prompt |
🚩 Useful Flags (ตอนรันคำสั่ง)
| Flag | ความหมาย |
|---|---|
--add-dir <path> | ให้สิทธิ์ Claude เข้าถึงโฟลเดอร์อื่นนอกโปรเจกต์ |
--agent <name> | เลือก agent สำหรับ session ปัจจุบัน |
--permission-mode plan | เริ่ม session ในโหมด plan ทันที |
--permission-mode bypassPermissions | ข้าม permission prompts ทั้งหมด (ใช้ด้วยความระวัง) |
--tools "Bash,Edit,Read" | จำกัดเครื่องมือที่ Claude ใช้ได้ใน session นี้ |
--system-prompt <text> | ระบุ System Prompt ใหม่ทั้งหมด แทนที่ค่าเริ่มต้น |
--append-system-prompt <text> | เพิ่มคำสั่งพิเศษเข้าไปต่อท้าย System Prompt เดิม |
--output-format json | บังคับให้ AI ตอบกลับเป็นโครงสร้าง JSON (ดีมากสำหรับ Automation) |
--output-format stream-json | ส่งผลลัพธ์แบบ stream เหมาะกับ automation ยาวๆ |
--verbose | แสดงผลแบบละเอียดขึ้น |
--worktree <name> | เปิด session ใน git worktree แยกสำหรับงานขนาน |
--remote-control | เปิด session ให้ควบคุมต่อจาก Claude app / web ได้ |
--teleport | ดึง web session กลับมาทำต่อใน local terminal |
--no-history | เริ่มเซสชันสะอาดโดยไม่โหลดประวัติเก่าเลย |
🧾 Commands จาก Official CLI Reference
| Command | การใช้งาน |
|---|---|
claude agents | ดูรายการ subagents ที่ตั้งค่าไว้ทั้งหมด |
claude auth logout | ออกจากระบบ Claude Code |
claude mcp | ตั้งค่า MCP servers จาก CLI |
claude plugin | จัดการ plugins ของ Claude Code |
claude remote-control | เปิด Remote Control server |
claude -c -p "query" | คุยต่อจาก session ล่าสุดแบบ print mode |
cat logs.txt | claude -p "explain" | ส่ง piped content ให้ Claude วิเคราะห์ |
👥 Agent Team & Remote Workflow
จาก docs ของ Claude Code มี flag และ workflow ที่เกี่ยวกับงานหลาย session/หลาย agent โดยตรง:
--teammate-mode autoใช้ค่า default สำหรับการแสดงผล teammate sessions--teammate-mode in-processแสดง teammate ใน process เดียว--teammate-mode tmuxใช้ tmux สำหรับงานที่แยกหลาย teammate ชัดๆ--worktree <name> --tmuxเหมาะกับการรันงานคู่ขนานหลาย feature branch
📦 Official Install & Auth Notes
| วิธี | คำสั่ง |
|---|---|
| Native install | curl -fsSL https://claude.ai/install.sh | bash |
| Homebrew | brew install --cask claude-code |
| WinGet | winget install Anthropic.ClaudeCode |
| Login ผ่าน Console billing | claude auth login --console |
🌟 จุดเด่นเฉพาะตัวของ Claude Code
- เด่นมากเรื่อง
skillsที่ตอนนี้รวมความสามารถของ custom commands เข้าไปแล้ว ทำให้ skill เดียวรองรับทั้ง reusable prompt, supporting files, shell injection และ subagent execution - มี
Agent Teamsและteammate-modeสำหรับงานคู่ขนานหลายเซสชันโดยตรง ซึ่งโฟลว์นี้ชัดมากใน ecosystem ของ Claude Code - มี
remote-controlและteleportสำหรับย้าย/ควบคุม session ข้ามระหว่าง local terminal กับ app/web - docs ให้โครง frontmatter ของ skills ละเอียดมาก เช่น
disable-model-invocation,user-invocable,allowed-tools,context: fork,paths,hooks - มี ecosystem ของ
CLAUDE.md, skills, hooks, plugins และ subagents ที่ผูกกันแน่นมาก เหมาะกับทีมที่อยากสร้างมาตรฐานการทำงานผ่าน prompt infrastructure
🖱 Keyboard Shortcuts (Interactive Mode)
Ctrl + C: ยกเลิกการตอบหรือการรันคำสั่ง Shell ของ ClaudeEsc: หยุดการกระทำปัจจุบันโดยไม่ปิดเซสชันAlt + Enter: ขึ้นบรรทัดใหม่ (Multiline Prompt)Alt + T: เปิด/ปิด Extended Thinking (โหมดคิดลึกสำหรับงานซับซ้อน)Tab: แนะนำชื่อไฟล์ (Autosuggest filenames)
📄 Configuration Files
| File | บทบาท |
|---|---|
CLAUDE.md | หัวใจสำคัญ! เก็บข้อมูล Tech Stack, Coding Rules, และคำสั่ง Build/Test |
.claude/settings.json | ตั้งค่าเฉพาะโปรเจกต์ เช่น รุ่นโมเดลที่ใช้บ่อย, Permission |
~/.claude/settings.json | ตั้งค่า Global สำหรับผู้ใช้งานทุกคนในเครื่อง |
🤖 Subagents & Agent Teams
Built-in Subagents จาก docs
| Subagent | ใช้ทำอะไร |
|---|---|
Explore | หาไฟล์ สำรวจ codebase แบบ read-only และกิน context น้อย |
Plan | วิจัย codebase ตอนอยู่ใน plan mode |
general-purpose | งานหลายขั้นตอนที่ต้องทั้งอ่านและลงมือทำ |
วิธีใช้งาน
- พิมพ์ชื่อ subagent ใน prompt เพื่อให้ Claude พิจารณา delegate
- ใช้
@mention เพื่อบังคับให้รัน subagent นั้นโดยตรง - ใช้
claude --agent <name>เพื่อให้ทั้ง session วิ่งด้วย subagent ตัวนั้น
วิธีเรียก Subagent ชัดๆ
| วิธี | ตัวอย่าง |
|---|---|
| สั่งเป็นภาษาธรรมชาติ | Use the code-reviewer subagent to look at my recent changes. |
บังคับด้วย @ mention | @"code-reviewer (agent)" look at the auth changes |
| ให้ทั้ง session ใช้ agent นั้น | claude --agent code-reviewer |
| ตั้งเป็น default ของโปรเจกต์ | ใส่ { "agent": "code-reviewer" } ใน .claude/settings.json |
หลักสำคัญจาก docs:
- ถ้าใช้ภาษาธรรมชาติ Claude จะเป็นคนตัดสินใจว่าจะ delegate หรือไม่
- ถ้าใช้
@mention จะเป็นการบังคับให้ subagent นั้นรันสำหรับงานนั้น - ถ้าใช้
--agentจะเปลี่ยนทั้ง session ให้ใช้ system prompt / model / tool restrictions ของ agent นั้น
Custom Subagent Files
| Scope | Path |
|---|---|
| Project | .claude/agents/ |
| User | ~/.claude/agents/ |
ตัวอย่างโครงไฟล์:
---
name: code-reviewer
description: Reviews code for quality and best practices
tools: Read, Glob, Grep
model: sonnet
---
You are a code reviewer. Focus on bugs, regressions, and maintainability.
Frontmatter สำคัญ
| Field | ความหมาย |
|---|---|
name | ชื่อ subagent |
description | บอกว่าเมื่อไรควรเรียก agent นี้ |
tools / disallowedTools | คุม tool access |
model | เช่น sonnet, opus, haiku |
permissionMode | เช่น plan, dontAsk, bypassPermissions |
skills | preload skills เข้า subagent |
hooks | lifecycle hooks เฉพาะ subagent |
memory | persistent memory scope |
Agent Teams
- ถ้าต้องการหลาย agent ทำงานขนานกันและสื่อสารกันจริง ให้ใช้ Agent Teams แทน subagents
- docs มี
--teammate-mode auto|in-process|tmuxสำหรับการแสดงผล teammate sessions
🧩 Skills & Custom Commands
Claude Code docs ระบุว่าตอนนี้ custom commands ถูกรวมเข้า skills แล้ว
| Scope | Path |
|---|---|
| Personal skill | ~/.claude/skills/<skill-name>/SKILL.md |
| Project skill | .claude/skills/<skill-name>/SKILL.md |
| Legacy commands | .claude/commands/*.md ยังใช้ได้ |
ตัวอย่าง skill:
---
name: deploy
description: Deploy the application to production
disable-model-invocation: true
---
Deploy $ARGUMENTS to production:
1. Run the test suite
2. Build the application
3. Push to the deployment target
Frontmatter สำคัญของ Skill
| Field | ใช้ทำอะไร |
|---|---|
name | ชื่อ skill / slash command |
description | อธิบายว่า skill นี้ใช้เมื่อไร |
disable-model-invocation | ให้ user เรียกเองเท่านั้น |
user-invocable | ซ่อนจาก / menu ได้ |
allowed-tools | tool ที่ใช้ได้ตอน skill active |
context: fork | ให้ skill ไปรันใน subagent context |
agent | เลือก subagent type ตอนใช้ context: fork |
วิธีเรียก Skill / Custom Command
| วิธี | ตัวอย่าง |
|---|---|
| เรียกตรงด้วย slash command | /deploy staging |
| ให้ Claude โหลดให้อัตโนมัติ | ถ้า description ตรงกับงาน Claude จะ invoke skill เอง |
| บังคับไม่ให้ Claude invoke เอง | ตั้ง disable-model-invocation: true |
| ให้ skill ไปรันใน subagent | ใช้ context: fork และระบุ agent |
Placeholder ที่ docs รองรับ
| Placeholder | ความหมาย |
|---|---|
$ARGUMENTS | argument ทั้งหมด |
$0, $1 | argument ตามตำแหน่ง |
| ``!`command``` | inject shell output เข้า skill |
${CLAUDE_SESSION_ID} | session id ปัจจุบัน |
${CLAUDE_SKILL_DIR} | path ของ skill directory |
⛓ Model Context Protocol (MCP)
Claude Code สามารถดึงข้อมูลจาก MCP servers ได้:
- Search Tools: Google Search, Brave Search
- Data Source: ดึงข้อมูลจาก Google Sheets, PostgreSQL, GitHub
- Communication: อ่าน/ส่งข้อความใน Slack, Gmail
⚡ Automation Recipes (Examples)
1) Fix Bug & Test
claude -p "แก้บั๊กใน @src/utils.ts โดยดู @logs/error.log แล้วรัน !npm test"
2) Feature Implementation (Plan Mode)
claude "วางแผนสร้างหน้า Dashboard ใหม่ตามมาตรฐานใน @CLAUDE.md โดยอ้างอิง @docs/spec.md"
3) Bulk Refactor
claude -p "เปลี่ยนการใช้ axios ทั้งหมดใน @src/services/ เป็น fetch API"
📅 Daily Pro Workflow (Team)
- Morning Sync:
claude -cเพื่อคุยต่อจากเมื่อคืน - Context Setup: ใช้
/initถ้าเริ่มงานในโปรเจกต์ใหม่ - Execution: ใช้
@fileเพื่อระบุขอบเขตงาน และรัน!commandเพื่อเช็กผล - Review: ให้ Claude รีวิวโค้ดด้วย
/reviewก่อนทำ Git Commit - Cost Control: เช็ก
/costก่อนจบงาน