Developer Tools

Claude Code
CLI Cheat Sheet

Complete reference for Claude Code CLI: commands, flags, keyboard shortcuts, permission modes, configuration files, MCP servers.

v4.6
Getting Started

Quick Start

1

Install

npm install -g @anthropic-ai/claude-code
2

Login

claude auth login
3

Start

claude

or claude "Fix the login bug"

Thuật ngữ

Glossary — hiểu nhanh các khái niệm

Những từ hay gặp trong Claude Code. Nắm các khái niệm này trước rồi đọc phần lệnh sẽ dễ hơn nhiều.

Phiên & Agent

SubagentAgent con làm task phụ, trả kết quả về
Background AgentPhiên chạy ngầm, giải phóng terminal
WorktreeGit worktree riêng để làm song song
Agent ViewMàn hình quản lý phiên nền & subagent

Kỹ năng & Lệnh

SkillPrompt đóng gói, gọi bằng slash command
WorkflowQuy trình đa-agent động, chạy nền
Slash CommandLệnh trong phiên, bắt đầu bằng /
HookScript tự chạy khi có sự kiện tool

Model & Ngữ cảnh

Effort LevelMức suy nghĩ: low → max → ultracode
Fast ModeOpus xuất nhanh hơn, không đổi model nhỏ
Extended ThinkingCho model suy luận sâu trước khi trả lời
Context / CompactCửa sổ ngữ cảnh; /compact nén để giải phóng
Prompt CachingCache ngữ cảnh lặp lại, giảm token

Chế độ & Tích hợp

Plan ModeChỉ-đọc, đề xuất kế hoạch trước khi sửa
Permission ModeDuyệt tool: plan / acceptEdits / auto
SandboxChạy code cách ly an toàn trong phiên
Checkpoint / RewindĐiểm lưu để cuộn code/hội thoại về
MCP ServerServer nối thêm tool cho Claude (MCP)
ArtifactSản phẩm độc lập chia sẻ được (design/HTML)
Reference

CLI Commands

Basic Usage

claudeStart interactive session
claude "query"Start with initial prompt
claude -p "query"Non-interactive (print & exit)
claude -cContinue most recent conversation
claude -r "name"Resume specific session
claude updateUpdate to latest version

Authentication

claude auth loginSign in
claude auth login --ssoForce SSO authentication
claude auth login --consoleUse API billing
claude auth logoutSign out
claude auth statusShow auth status

Model & Session

--model opusUse Opus model
--model sonnetUse Sonnet model
--effort highIncrease thinking effort
--effort maxMaximum thinking (Opus only)
-n "name"Name the session
--fork-sessionNew session ID from resume

Permission & Security

--permission-mode planRead-only, plan proposals
--permission-mode acceptEditsAuto-approve edits
--permission-mode autoAuto-approve all
--allowedTools "Read,Edit"Pre-approve tools
--disallowedTools "Bash(rm *)"Block tools

Output & Format

--output-format textPlain text (default)
--output-format jsonJSON with metadata
--output-format stream-jsonStreaming JSON
--json-schema '...'Structured output
--verboseVerbose logging
--debugDebug mode

Advanced

--bareSkip auto-discovery (fast)
--max-turns 10Limit agentic turns
--max-budget-usd 5Budget limit
-w feature-authIsolated git worktree
--add-dir /pathAdd working directory
--remote "task"Create web session
In-Session

Slash Commands

Gõ / để xem danh sách. Skill (/test, /code-review…) hoạt động như prompt gửi tới Claude; Workflow (/deep-research) toả việc ra nhiều subagent chạy nền. Từ v2.1.199+ có thể nối tối đa 6 skill: /skill-a /skill-b làm XYZ.

Cách đọc cú pháp

<arg>Bắt buộc
[arg]Tùy chọn
[a|b]Chọn một
/a /b taskNối nhiều skill (tối đa 6)

Phiên & Điều hướng

/helpDanh sách lệnh khả dụng
/clearBắt đầu mới, giữ memory (alias /new)
/resume [name]Quay lại phiên trước
/rewind [--list]Cuộn code/hội thoại về checkpoint
/branch [name]Rẽ nhánh hội thoại thử hướng khác
/fork [prompt]Sao chép phiên sang background
/compactNén context, có thể kèm chỉ dẫn
/export [file]Xuất hội thoại ra text

Model & Ngữ cảnh

/model [model]Đổi model, lưu default
/effort [level]low → max → ultracode / auto
/fast [on|off]Bật/tắt fast mode (mid-turn)
/thinking [on|off]Bật/tắt extended thinking
/context [all]Xem mức dùng context
/usageXem API usage (alias /cost)
/statusTrạng thái phiên & model

Cấu hình & Chẩn đoán

/config [k=v]Mở settings / đặt trực tiếp
/permissionsQuản lý allow/ask/deny tool
/mcpQuản lý MCP server & OAuth
/memorySửa CLAUDE.md & auto memory
/initTạo CLAUDE.md cho dự án
/hooksXem cấu hình hook
/doctorChẩn đoán cài đặt (alias /checkup)
/agentsTạo/quản lý subagent

Review & Test (Skills)

/code-review [lvl] [--fix]Soát lỗi diff/PR; ultra = cloud
/security-review [--fix]Quét lỗ hổng bảo mật
/simplify [--fix]Đơn giản hóa code
/test [--fix]Phân tích test fail, đề xuất fix
/verifyXác minh code/test chạy đúng
/debug [mô tả]Bật debug log, truy vết sự cố
/suggestGợi ý cải tiến code

Skills & Workflow khác

/design [brief]Mockup UI/landing trên canvas
/dataviz [request]Hướng dẫn thiết kế chart/dashboard
/deep-research <q>Toả web search, tổng hợp báo cáo
/loop [interval] [prompt]Chạy lặp theo chu kỳ (alias /proactive)
/batch <instruction>Tách việc lớn thành 5–30 phần song song
/subtask [task]Giao task phụ cho subagent

Chạy nền & Thiết bị

/tasksLiệt kê việc nền của phiên
/background [prompt]Tách phiên chạy ngầm (alias /bg)
/teleportKéo web session về terminal
/remote-control [on|off]Điều khiển phiên từ thiết bị khác
/watch <command>Chạy Bash lặp, đẩy output cho Claude
/desktopTiếp tục phiên trong app Desktop

Ký hiệu đặc biệt

!npm testChạy lệnh Bash trực tiếp
#fix bugVào plan mode
@file.tsTham chiếu một file
@file.ts#5-10Tham chiếu khoảng dòng
@src/components/Tham chiếu thư mục
Shortcuts

Keyboard Shortcuts

Global Actions

Cancel / ExitCtrl+C
Exit Claude CodeCtrl+D
Toggle task listCtrl+T
History searchCtrl+R
Toggle verboseCtrl+O

Chat Input

Submit messageEnter
New lineShift+Enter
Accept autocompleteTab
Cycle permission modesShift+Tab
Clear prompt inputCtrl+L
Stash current promptCtrl+S
Paste imageCtrl+V
Open external editorCtrl+G
Voice dictationHold Space

Model & Mode

Open model pickerCmd+P
Toggle fast modeMeta+O
Toggle thinkingCmd+T
Undo last actionCtrl+_
Kill background agentsCtrl+X Ctrl+K

Permission Prompts

Confirm actionY or Enter
Decline actionN or Escape
Toggle explanationCtrl+E
Toggle debug infoCtrl+D
Security

Permission Modes

defaultSafe

Reads only. Best for getting started and sensitive work.

acceptEditsRecommended

Auto-approve reads + file edits + common FS commands (mkdir, touch, mv, cp, rm).

planRead-only

Reads only, generates plan proposals. Use for exploring before editing.

autoFull access

Auto-approve everything with background safety checks. Best for long tasks.

Switch modes with Shift+Tab or --permission-mode <mode> at startup. Protected paths (.git/, .zshrc, .mcp.json) are never auto-approved.

Setup

Configuration Files

CLAUDE.mdInstructions

Persistent project/user instructions loaded at session start.

./CLAUDE.md project-level
~/.claude/CLAUDE.md user-level
./.claude/rules/*.md path-scoped
settings.jsonSettings

Permissions, hooks, environment variables, model defaults.

~/.claude/settings.json user
.claude/settings.json project
.claude/settings.local.json local
.mcp.jsonMCP

MCP server configuration for external tools integration.

claude mcp add name transport url
claude mcp list
claude mcp remove name
.claude/Directory

Project configuration directory structure.

settings.json shared config
commands/ custom slash commands
agents/ subagent definitions
keybindings.json custom keys
Automation

Hooks

Shell commands that run automatically in response to Claude Code events. Defined in settings.json.

Available Hooks

SessionStartWhen session begins
SessionEndWhen session ends
UserPromptSubmitBefore user message processed
PreToolUseBefore tool execution
PermissionRequestOn permission prompt
InstructionsLoadedWhen CLAUDE.md loaded

Environment Variables

ANTHROPIC_API_KEYAPI key for auth
CLAUDE_CODE_DEBUG_LOGS_DIRDebug log directory
CLAUDE_CODE_DISABLE_AUTO_MEMORYDisable auto memory
CLAUDE_CODE_SIMPLESkip context discovery
Best Practices

Golden Rules

1

Review diff trước khi accept

Luôn đọc kỹ thay đổi trước khi nhấn Y. Claude rất mạnh nhưng không hoàn hảo — bạn là người kiểm soát cuối cùng.

2

Prompt có cấu trúc

Theo công thức: WHAT (làm gì) + WHERE (file/folder nào) + HOW (cách làm) + CHECK (kiểm tra thế nào).

Fix the login redirect bug in src/auth/login.ts.
Use NextAuth's signIn callback, not manual redirect.
Verify by running npm test -- auth.
3

Plan Mode cho task phức tạp

Prefix # trước prompt để vào Plan Mode. Claude chỉ đọc, phân tích và đề xuất — không sửa code. Review plan rồi mới thực thi.

# Refactor authentication to use JWT instead of sessions
4

Tạo CLAUDE.md cho mọi project

Chạy /init để tạo file hướng dẫn. Claude đọc file này mỗi session — giúp nó hiểu stack, conventions, và quy tắc của project.

5

Dùng /compact khi context đầy

Context window có giới hạn. Khi thấy Claude quên context cũ hoặc phản hồi chậm, chạy /compact để nén conversation mà vẫn giữ ý chính.

6

Commit thường xuyên

Commit sau mỗi milestone nhỏ. Nếu Claude làm sai, bạn có thể git revert dễ dàng thay vì undo thủ công hàng chục file.

Context

Context & Model

Context Management

/compactNén context, giữ lại ý chính
/compact "focus"Nén + hướng focus cụ thể
/clearXóa sạch context (bắt đầu lại)
/costXem token usage & chi phí
@file.tsĐính kèm file vào context
@folder/Đính kèm cả folder
Ctrl+VPaste ảnh/screenshot vào prompt

Chọn Model

OpusMạnh nhất, tốt cho task phức tạp
SonnetCân bằng tốc độ & chất lượng
HaikuNhanh nhất, task đơn giản
/modelĐổi model giữa session
--effort high|maxTăng thinking depth
/fastToggle fast mode (Opus)

Khi nào /compact vs /clear

/compactCùng task, context quá dài
/clearChuyển sang task hoàn toàn mới
claude -cTiếp tục session cũ (đã exit)
claude -r "name"Quay lại session theo tên

Tiết kiệm Context

--bareBỏ qua CLAUDE.md, git info
@file.ts#5-20Chỉ đính kèm dòng 5–20
#Plan mode: chỉ đọc, không sửa
--max-turns 5Giới hạn số lượt agent
Workflows

Quy trình thực tế

Mở project, code, commit, lặp lại
$ cd my-project && claude
# Mở project + bắt đầu session
> Fix bug login redirect sau khi auth @src/auth/
# Mô tả task + @mention file/folder liên quan
⌘ Review diff → Y để accept, N để từ chối
# Luôn đọc diff trước khi accept
> !npm test
# Chạy test trực tiếp trong session
> Commit fix login redirect
# Bảo Claude commit sau mỗi milestone
> /compact
# Nén context → tiếp task tiếp theo
Paste error, Claude trace, fix, test
> Fix this error: "TypeError: Cannot read property 'id' of undefined"
# Paste error message hoặc screenshot (Ctrl+V)
⌘ Claude tự grep → đọc file → trace root cause
# Để Claude tự tìm — không cần chỉ file
⌘ Review diff → Y hoặc "thử cách khác"
# Không hài lòng? Nói Claude thử approach khác
> !npm test -- auth
# Chạy test liên quan để verify fix
> Có edge case nào khác không? Kiểm tra thêm
# Nhờ Claude rà soát thêm edge cases
Plan trước, code sau, test liên tục
⌘ Shift+Tab → Plan
# Plan Mode — KHÔNG sửa code
> Phân tích @src/module/ — dependencies, side effects
# Hiểu impact trước khi động vào
⌘ Shift+Tab → Auto
# Thoát Plan → bắt đầu refactor từng bước
> !npm test
# Test pass? Tiếp tục
> Commit refactor module X
# Bảo Claude commit sau mỗi bước thành công
> /compact
# Nén context → tiếp phần tiếp theo
Setup CLAUDE.md từ đầu
$ mkdir my-app && cd my-app && claude
# Tạo thư mục + mở Claude Code
> /init
# Claude phân tích codebase → tự tạo CLAUDE.md
⌘ Shift+Tab → Plan
# Plan Mode — thiết kế kiến trúc
> Thiết kế folder structure, key files, conventions cho React + Tailwind
# Claude đề xuất cấu trúc
⌘ Shift+Tab → Auto
# Thoát Plan → scaffold dự án
> !git init && git add -A && git commit -m "init"
# Commit baseline
Đúng model, đúng việc
> /model haiku
# Haiku cho fix nhỏ, rename, typo — rẻ nhất
> /fast
# Fast mode — cùng model, output nhanh hơn
> /model sonnet
# Sonnet cho code thực — best value
> /compact
# Compact thường xuyên = ít token = tiết kiệm
> /model opus
# CHỈ dùng Opus cho: race condition, architecture, debug khó
> /cost
# Kiểm tra chi phí — chỉ hiển thị khi dùng API
Phương pháp

Quy trình 5 file — để app không “vỡ” khi sửa

Đây là cách làm việc cùng Claude Code theo hướng documentation-driven: mỗi quyết định được ghi thành một file trong dự án, không trôi vào chat. Bạn đóng vai Product Owner + BA + QA, Claude là cả team kỹ thuật. Rút gọn từ series “Đồng Use Claude”.

Vấn đề

Chat với Claude → sinh code → đi tiếp. Lần sửa sau, không ai (kể cả AI) nhớ ràng buộc ban đầu, nên một thay đổi làm vỡ nhiều chỗ. Quyết định nằm trong lịch sử chat, không nằm trong dự án → bảo trì bất khả thi.

1
INTENT
intent/INTENT-01.md

Vì sao làm? Bài toán kinh doanh thật, kết quả quan sát được, ràng buộc tuyệt đối. “Hợp đồng” đầu tiên trước khi thiết kế.

Bạn viết mục tiêu & ràng buộc
Claude hỏi lại chỗ mơ hồ
2
SPEC
spec/SPEC-01.md

Biến INTENT thành yêu cầu chức năng, business rule, data schema, và các trade-off có ghi rõ lý do chọn.

Bạn duyệt tính năng & rule
Claude dịch intent thành spec
3
PLAN
plan/PLAN-01.md

Biến SPEC thành kế hoạch thi công: file nào đổi, thứ tự thực thi, rủi ro & cách giảm, tiêu chí hoàn thành.

Bạn duyệt thứ tự & rủi ro
Claude lập timeline & list file
4
BUILD
CLAUDE.md + code

Claude viết code bám theo PLAN. CLAUDE.md giữ convention & chỉ dẫn để mọi phiên làm nhất quán.

Bạn review diff, đi tiếp hay sửa
Claude code đúng kế hoạch
5
TEST
test/TEST-01.md

Chạy kịch bản test đã viết, có bằng chứng. Thay “cảm giác chắc chạy” bằng “biết chắc chạy”.

Bạn chấp nhận hoặc từ chối
Claude chạy test, nộp bằng chứng
+
DEPLOY & WORKBOOK
phần 7 & 8

Deploy lên Cloudflare (chi phí 0đ) rồi chạy lại toàn bộ kịch bản test trên URL thật; đóng gói tính năng phức tạp thành component tái dùng.

Thứ tự là bất biến

Thiếu INTENTSPEC thành đoán mò
Thiếu SPECPLAN thiếu business logic
Thiếu PLANBUILD làm tán loạn
Thiếu TESTChỉ “cảm thấy” chứ không “biết”

Phân vai rõ ràng

BạnProduct Owner + BA + QA
ClaudeDev + Tester + PM trong một
File .md“Hợp đồng” để đối chiếu
Nguyên tắcAI không tự quyết yêu cầu thay bạn
Ví dụ xuyên suốt series

Mini-CRM cho chủ shop: khách bị trùng bản ghi giữa Excel & sổ tay, số điện thoại mỗi nơi một định dạng. App chuẩn hoá số điện thoại và phát hiện/gộp bản ghi trùng — business logic thật, không phải demo đồ chơi. Mỗi quyết định neo vào file để “bạn của tương lai” (hoặc bất kỳ AI nào) dựng lại ngữ cảnh sau nhiều tháng.

Học full series “Đồng Use Claude” →
Ví dụ thực tế

Dự án từ A–Z: Landing Page cho khách hàng

Toàn bộ chỉ dùng lệnh Claude Code gốc — không cần plugin. Ngày 1 dựng landing page, ngày 2 thêm form đặt hàng và hoàn thiện.

Ngày 1 — Khởi tạo & dựng trang

day-1-build.sh
$ claude "Khởi tạo landing page spa bằng Astro + Tailwind"
# Mở phiên kèm prompt đầu tiên
> /plan "Hero, dịch vụ, testimonials, responsive; chưa cần form"
# Vào plan mode, duyệt kế hoạch trước khi cho sửa file
> Dựng hero + phần dịch vụ theo plan
# Prompt tự nhiên, Claude sinh code theo kế hoạch
! npm run dev
# Tiền tố ! chạy Bash trực tiếp để xem thử ở localhost
> Commit theo chuẩn conventional rồi push
# commit: "feat: add spa landing hero, services, testimonials"

Ngày 2 — Thêm form đặt hàng & hoàn thiện

day-2-order-form.sh
$ claude -c
# Nối tiếp phiên gần nhất, giữ nguyên context hôm qua
> @src/pages/index.astro Thêm form đặt hàng: tên, SĐT, gói dịch vụ, ghi chú
# @file trỏ đúng file cần sửa để Claude không đoán mò
> Validate SĐT, submit qua fetch tới webhook, chuyển hướng /cam-on khi thành công
# Chỉnh sửa thêm phần xử lý & submit cho form
> /diff
# Xem lại toàn bộ thay đổi trước khi review
> /code-review high --fix
# Soát accessibility, validate, edge case — tự áp bản vá
> /security-review
# Form nhận input người dùng: quét lỗ hổng trước khi ship
> Commit & push, sẵn sàng deploy
# Nếu lỡ tay, /rewind cuộn code về checkpoint an toàn
macOS Terminal

cmux — Terminal cho AI Agents

Chạy nhiều Claude Code sessions cùng lúc trên macOS? Dùng cmux — Ghostty-based terminal với vertical tabs, smart notifications, và built-in browser.

Vertical tabs Git branch, PR status, directory per tab
Notifications Blue ring khi Claude cần attention — ⌘⇧U nhảy ngay
Split panes ⌘D split right, ⌘⇧D split down
Browser pane ⌘⇧L mở browser ngay trong terminal
cmux Cheat Sheet → GitHub ↗
Starter Kit

AgentKit — AI Development Team

Supercharge Claude Code với 45 specialized agents, 108+ skills, và production-ready workflows. Ship features in minutes thay vì hours. (AgentKit là tên mới của ClaudeKit.)

45 Agents Planner, Debugger, Tester, Code Reviewer, UI/UX Designer...
108+ Skills Frontend, backend, DevOps, cloud, AI, media processing
Workflow Commands /ak:plan → /ak:cook → /ak:test → ship
Thinking Skills Systematic debugging, root cause tracing, problem-solving frameworks
AgentKit Cheat Sheet → Get AgentKit ↗
Integrations

IDE Shortcuts

VS Code

Toggle focus editor/ClaudeCmd+Esc
New conversationCmd+Shift+Esc
Insert @-mentionOption+K
Model pickerCmd+P

JetBrains

Open Claude CodeCmd+Esc
Insert file referenceCmd+Option+K
Connect terminalclaude /ide