กลับไปหน้าสูตร
#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)

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 installcurl -fsSL https://claude.ai/install.sh | bash
Homebrewbrew install --cask claude-code
WinGetwinget install Anthropic.ClaudeCode
Login ผ่าน Console billingclaude 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 ของ Claude
  • Esc: หยุดการกระทำปัจจุบันโดยไม่ปิดเซสชัน
  • 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

ScopePath
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
skillspreload skills เข้า subagent
hookslifecycle hooks เฉพาะ subagent
memorypersistent 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 แล้ว

ScopePath
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-toolstool ที่ใช้ได้ตอน 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ความหมาย
$ARGUMENTSargument ทั้งหมด
$0, $1argument ตามตำแหน่ง
``!`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)

  1. Morning Sync: claude -c เพื่อคุยต่อจากเมื่อคืน
  2. Context Setup: ใช้ /init ถ้าเริ่มงานในโปรเจกต์ใหม่
  3. Execution: ใช้ @file เพื่อระบุขอบเขตงาน และรัน !command เพื่อเช็กผล
  4. Review: ให้ Claude รีวิวโค้ดด้วย /review ก่อนทำ Git Commit
  5. Cost Control: เช็ก /cost ก่อนจบงาน