Chương này gom các thao tác làm việc hằng ngày với Claude Code: common workflows (recipe prompt cho từng loại task), best practices để quản lý context, interactive mode và bảng tra keybinding. Độc giả đã biết cơ bản, nên phần này viết dạng tra cứu nhanh.
3.1 Prompt recipes cho task hằng ngày
Mỗi recipe là một pattern prompt, hoạt động trên mọi surface của Claude Code. Điều chỉnh câu chữ theo project của bạn.
Hiểu codebase mới
Đi từ câu hỏi rộng đến hẹp:
give me an overview of this codebase
explain the main architecture patterns used here
what are the key data models?
how is authentication handled?
- Hỏi về coding conventions và patterns đang dùng.
- Yêu cầu một glossary các thuật ngữ riêng của project.
- Cài code intelligence plugin cho ngôn ngữ của bạn để Claude có "go to definition" / "find references" chính xác.
Tìm code liên quan
find the files that handle user authentication
how do these authentication files work together?
trace the login process from front-end to database
Nói cụ thể, dùng domain language của project.
Fix bug hiệu quả
I'm seeing an error when I run npm test
suggest a few ways to fix the @ts-ignore in user.ts
update user.ts to add the null check you suggested
- Cung cấp lệnh để reproduce và stack trace.
- Nói rõ bug intermittent hay consistent.
Refactor code
find deprecated API usage in our codebase
suggest how to refactor utils.js to use modern JavaScript features
refactor utils.js to use ES2024 features while maintaining the same behavior
run tests for the refactored code
Làm từng bước nhỏ, testable; yêu cầu giữ backward compatibility khi cần.
Viết test
find functions in NotificationsService.swift that are not covered by tests
add tests for the notification service
add test cases for edge conditions in the notification service
run the new tests and fix any failures
Claude đọc các test file có sẵn để bám theo style, framework và assertion pattern đang dùng. Yêu cầu Claude chỉ ra edge case dễ bỏ sót (error condition, boundary value, input bất thường).
Tạo pull request
Cách nhanh: nói thẳng create a pr for my changes. Hoặc từng bước:
summarize the changes I've made to the authentication module
create a pr
enhance the PR description with more context about the security improvements
Khi PR được tạo bằng gh pr create, session tự động được link với PR đó. Tìm lại bằng claude --from-pr 1234 (mở session picker lọc theo PR), hoặc dán URL PR vào ô search của /resume.
Xử lý documentation
find functions without proper JSDoc comments in the auth module
add JSDoc comments to the undocumented functions in auth.js
improve the generated documentation with more context and examples
check if the documentation follows our project standards
Nói rõ style (JSDoc, docstrings...), yêu cầu ví dụ trong doc.
Làm việc với image
Ba cách đưa image vào conversation:
- Drag & drop image vào cửa sổ Claude Code.
- Copy image rồi paste bằng
Ctrl+V(trên macOS/iTerm2 dùng đượcCmd+V). - Cung cấp path:
Analyze this image: /path/to/your/image.png.
Dùng cho screenshot lỗi, UI design, diagram:
Here's a screenshot of the error. What's causing it?
Generate CSS to match this design mockup
Khi Claude tham chiếu [Image #1], Cmd+Click (Mac) / Ctrl+Click (Win/Linux) để mở image.
Reference file và directory với @
Explain the logic in @src/utils/auth.js
What's the structure of @src/components?
Show me the data from @github:repos/owner/repo/issues
- File reference nhét toàn bộ nội dung file vào context; directory reference chỉ hiện listing, không hiện nội dung.
- Gõ
@mở path suggestion menu;EnterhoặcTabđể chấp nhận path highlight,Enterlần nữa để gửi. @file reference tự kéo thêmCLAUDE.mdở thư mục file và các thư mục cha vào context.- Reference nhiều file trong một message:
@file1.js and @file2.js.
Chạy Claude theo lịch
| Option | Chạy ở đâu | Hợp cho |
|---|---|---|
| Routines | Hạ tầng Anthropic quản lý | Task chạy kể cả khi máy tắt; trigger được bằng API call / GitHub event |
| Desktop scheduled tasks | Máy bạn, qua desktop app | Task cần truy cập file/tool local hoặc uncommitted changes |
| GitHub Actions | CI pipeline | Task gắn với repo event (PR mở) hoặc cron cạnh workflow config |
/loop | Session CLI hiện tại | Polling nhanh khi session đang mở; --resume/--continue khôi phục task chưa hết hạn |
Prompt cho scheduled task phải nói rõ "success" là gì và làm gì với kết quả — task chạy tự động, không hỏi lại được.
Hỏi Claude về chính nó
Claude có sẵn quyền truy cập documentation của nó, luôn là bản mới nhất bất kể version bạn đang dùng:
can Claude Code create pull requests?
how does Claude Code handle permissions?
what skills are available?
what are the limitations of Claude Code?
Dùng /help để xem command hiện có, hoặc mở Claude Code docs để đọc hướng dẫn mới nhất.
3.2 Workflow đa phiên và ngoài interactive
Resume conversation
Task kéo dài nhiều buổi — nối lại thay vì giải thích lại từ đầu. Claude Code lưu mọi conversation cục bộ.
claude --continue # resume session gần nhất trong thư mục hiện tại
claude --resume # chọn từ danh sách
Trong session đang chạy dùng /resume. Nếu chưa có session nào, --continue in No conversation found to continue rồi thoát.
Parallel sessions với worktrees
Mỗi git worktree là một checkout riêng trên branch riêng, tạo từ một commit đã có — repo cần ít nhất một commit.
claude --worktree feature-auth
Chạy lệnh tương tự với tên khác ở terminal thứ hai để có session song song độc lập. Repo chưa có commit sẽ báo Failed to resolve base branch "HEAD".
Plan before editing (plan mode)
Với thay đổi muốn review trước khi ghi ra disk: Claude đọc file và đề xuất plan nhưng không edit cho tới khi bạn duyệt. Status bar hiện ⏸ plan mode on.
claude --permission-mode plan
Giữa session bấm Shift+Tab để cycle sang plan mode. Chu trình: default → acceptEdits → plan.
Delegate research cho subagent
Explore codebase lớn làm đầy context bằng các lần đọc file. Giao việc đó cho subagent để chỉ nhận về findings:
use a subagent to investigate how our auth system handles token refresh
Subagent đọc file trong context window riêng và báo cáo tóm tắt về.
Pipe Claude vào script (non-interactive)
Chạy Claude non-interactive cho CI, pre-commit hook, batch processing. Stdin/stdout hoạt động như mọi Unix tool.
git log --oneline -20 | claude -p "summarize these recent commits"
claude -p "List all API endpoints" --output-format json
claude -p "Analyze this log file" --output-format stream-json --verbose
json: trả một JSON object có fieldresult.stream-json: in một JSON object mỗi dòng, bắt đầu bằng init event.
3.3 Best practices — quản lý context là gốc
Gần như mọi best practice xuất phát từ một ràng buộc: context window đầy nhanh, và performance giảm khi nó đầy. Context giữ toàn bộ conversation — mọi message, mọi file Claude đọc, mọi output lệnh. Khi gần đầy, Claude có thể "quên" chỉ dẫn trước đó hoặc mắc lỗi nhiều hơn.
Cho Claude cách tự verify công việc
Claude dừng khi công việc "trông có vẻ xong". Cho nó một check trả về pass/fail (test suite, build exit code, linter, screenshot so với design) thì vòng lặp tự đóng: Claude làm, chạy check, đọc kết quả, lặp tới khi pass.
| Chiến lược | Trước | Sau |
|---|---|---|
| Cung cấp tiêu chí verify | "implement a function that validates email addresses" | "write a validateEmail function. example test cases: ... run the tests after implementing" |
| Verify UI trực quan | "make the dashboard look better" | "[paste screenshot] implement this design. take a screenshot and compare it, list differences and fix" |
| Trị gốc, không trị triệu chứng | "the build is failing" | "the build fails with [error]. fix and verify the build succeeds. address the root cause, don't suppress" |
Mức độ "gate" khi dừng: trong một prompt (yêu cầu chạy check ngay) → xuyên session với /goal condition → deterministic gate bằng Stop hook → second opinion bằng verification subagent. Yêu cầu Claude trưng bằng chứng (output test, lệnh đã chạy) thay vì chỉ khẳng định "thành công".
Explore → Plan → Code → Commit
Để Claude nhảy thẳng vào code dễ giải sai vấn đề. Bốn pha:
- Explore — vào plan mode, Claude đọc file và trả lời, không sửa gì.
- Plan — yêu cầu tạo implementation plan chi tiết. Bấm
Ctrl+Gđể mở plan trong text editor sửa trực tiếp trước khi Claude tiến hành. - Implement — thoát plan mode, để Claude code và verify theo plan.
- Commit — yêu cầu commit với message mô tả và mở PR.
Plan mode có overhead. Với task rõ scope, fix nhỏ (sửa typo, thêm log line, đổi tên biến) — bảo Claude làm thẳng. Nếu bạn diễn tả được diff trong một câu, bỏ qua plan.
Cung cấp context cụ thể trong prompt
Càng chính xác càng ít phải sửa. Reference file cụ thể, nêu ràng buộc, chỉ pattern mẫu.
| Chiến lược | Trước | Sau |
|---|---|---|
| Scope task | "add tests for foo.py" | "write a test for foo.py covering the edge case where the user is logged out. avoid mocks." |
| Chỉ nguồn | "why does ExecutionFactory have such a weird api?" | "look through ExecutionFactory's git history and summarize how its api came to be" |
| Tham chiếu pattern có sẵn | "add a calendar widget" | "look at how existing widgets are implemented... HotDogWidget.php is a good example. follow the pattern..." |
| Mô tả triệu chứng | "fix the login bug" | "login fails after session timeout. check src/auth/, especially token refresh. write a failing test, then fix it" |
Cung cấp rich content: reference file bằng @, paste image, cho URL doc (dùng /permissions allowlist domain hay dùng), pipe data (cat error.log | claude).
Viết CLAUDE.md hiệu quả
/init sinh CLAUDE.md khởi đầu dựa trên cấu trúc project (detect build system, test framework, code pattern), rồi tinh chỉnh dần. CLAUDE.md được đọc mỗi đầu conversation — chỉ đưa vào thứ áp dụng rộng. Với domain knowledge chỉ thỉnh thoảng cần, dùng skills. Chạy /context để xác nhận Claude đã load file.
Giữ ngắn gọn. Với mỗi dòng, hỏi: "Bỏ dòng này thì Claude có mắc lỗi không?" Nếu không, cắt. CLAUDE.md phình to khiến Claude bỏ qua chính các chỉ dẫn của bạn.
| ✅ Nên có | ❌ Nên bỏ |
|---|---|
| Bash command Claude không đoán được | Thứ Claude tự suy ra khi đọc code |
| Code style khác mặc định | Convention chuẩn Claude đã biết |
| Hướng dẫn test, test runner ưa dùng | Chi tiết API doc (link thay vì chép) |
| Repo etiquette (branch naming, PR convention) | Thông tin thay đổi thường xuyên |
| Quyết định kiến trúc riêng của project | Mô tả từng file của codebase |
| Env quirk (env var bắt buộc) | Điều hiển nhiên như "write clean code" |
Vị trí đặt CLAUDE.md: ~/.claude/CLAUDE.md (mọi session), ./CLAUDE.md (check vào git, chia sẻ team), ./CLAUDE.local.md (ghi chú riêng, thêm vào .gitignore), thư mục cha (monorepo), thư mục con (kéo vào khi Claude đọc file trong đó). Import file khác bằng cú pháp @path/to/import.
Giảm interruption với permissions
Mặc định Claude Code xin phép cho hành động sửa hệ thống. Ba cách giảm:
- Auto mode: một classifier model review lệnh, chỉ chặn cái rủi ro (scope escalation, hạ tầng lạ, hành động do nội dung độc hại). Chạy:
claude --permission-mode auto -p "fix all lint errors". - Permission allowlist:
/permissionscho phép tool an toàn cụ thể (npm run lint,git commit). - Sandboxing:
/sandboxcô lập OS-level (filesystem, network).
Các mở rộng khác
- CLI tools: bảo Claude dùng
gh,aws,gcloud,sentry-cli— cách context-efficient nhất để tương tác dịch vụ ngoài. Không cógh, request unauthenticated dễ dính rate limit. - MCP servers:
claude mcp addđể nối Notion, Figma, database. - Hooks: cho hành động phải xảy ra mọi lần, không ngoại lệ (deterministic, khác CLAUDE.md chỉ mang tính khuyến nghị). Sửa
.claude/settings.json, xem bằng/hooks. - Skills:
SKILL.mdtrong.claude/skills/cho domain knowledge và workflow lặp lại. Dùngdisable-model-invocation: truecho workflow có side effect chỉ trigger thủ công. - Subagents: định nghĩa trong
.claude/agents/, chạy context riêng với tool riêng. - Plugins:
/pluginduyệt marketplace.
Let Claude interview you
Với feature lớn, để Claude phỏng vấn bạn trước bằng AskUserQuestion tool:
I want to build [brief description]. Interview me in detail using the AskUserQuestion tool.
Ask about technical implementation, UI/UX, edge cases, concerns, and tradeoffs.
Keep interviewing until we've covered everything, then write a complete spec to SPEC.md.
Spec xong, mở session mới để execute với context sạch. Spec tốt nhất là self-contained: nêu tên file/interface, nói rõ cái gì ngoài scope, kết bằng bước verify end-to-end.
Quản lý session
- Course-correct sớm: bấm
Escđể dừng Claude giữa chừng (context được giữ, redirect được).Esc + Eschoặc/rewindmở rewind menu. Nói"Undo that"để Claude revert. /cleargiữa các task không liên quan: session dài chứa context lạc đề làm giảm performance.- Sửa quá 2 lần cùng một lỗi → context đã bị nhiễu approach thất bại →
/clearvà viết prompt mới cụ thể hơn. - Compact: khi gần giới hạn, Claude tự compact, giữ code và quyết định quan trọng. Kiểm soát thêm bằng
/compact <instructions>(vd/compact Focus on the API changes). - Checkpoints: mỗi prompt tạo một checkpoint; Claude snapshot file trước mỗi thay đổi.
Esc + Esc//rewindđể restore conversation, code, hoặc cả hai. Lưu ý: checkpoint chỉ track thay đổi qua file editing tool của Claude — thay đổi qua Bash hoặc process ngoài không được ghi. Không thay thế git. - Subagent để điều tra:
"use subagents to investigate X"— explore ở context riêng, giữ main conversation sạch.
Automate và scale
- Non-interactive:
claude -p "prompt"cho CI/hook/script. Run vẫn tạo resumable session trừ khi--no-session-persistence. - Parallel sessions: Worktrees, Desktop app, Claude Code on the web, Agent teams — chọn theo mức coordination muốn.
- Writer/Reviewer pattern: một Claude viết code, một Claude context sạch review (không bị thiên vị code nó vừa viết).
- Fan out across files: loop
claude -pcho từng file, dùng--allowedToolsgiới hạn quyền cho batch chạy unattended.
for file in $(cat files.txt); do
claude -p "Migrate $file from React to Vue. Return OK or FAIL." \
--allowedTools "Edit,Bash(git commit *)"
done
- Adversarial review step: trước khi coi là xong, cho subagent context sạch review diff và báo cáo gap. Dùng skill
/code-reviewcho correctness, hoặc tự viết prompt review diff theo PLAN.md. Nhắc reviewer chỉ flag gap ảnh hưởng correctness hoặc requirement — reviewer luôn tìm ra gì đó, chạy theo mọi finding dẫn tới over-engineering.
Các failure pattern thường gặp
| Pattern | Triệu chứng | Fix |
|---|---|---|
| Kitchen sink session | Trộn nhiều task không liên quan trong một session | /clear giữa các task |
| Correcting over and over | Sửa đi sửa lại cùng lỗi, context nhiễm approach hỏng | Sau 2 lần sửa thất bại, /clear + prompt tốt hơn |
| Over-specified CLAUDE.md | File quá dài, Claude bỏ qua nửa số rule | Prune mạnh tay hoặc chuyển thành hook |
| Trust-then-verify gap | Code trông hợp lý nhưng không handle edge case | Luôn cung cấp verify (test, script, screenshot) |
| Infinite exploration | "investigate" không scope, Claude đọc hàng trăm file | Scope hẹp hoặc dùng subagent |
3.4 Interactive mode — tính năng hằng ngày
Shell mode với !
Chạy shell command trực tiếp, không qua Claude, bằng cách prefix !:
! npm test
! git status
- Thêm command và output vào context.
- Từ v2.1.186, Claude tự phản hồi output ngay khi nó vào transcript (chạy
! npm testlà được giải thích failure luôn). Tắt bằngrespondToBashCommands: falsetrongsettings.json. - Hỗ trợ history-based autocomplete (gõ một phần rồi
Tab) và file path autocomplete (gõ token có/). - Thoát bằng
Escape,Backspace, hoặcCtrl+Ukhi prompt rỗng.
Background Bash commands
Bấm Ctrl+B (tmux: bấm hai lần) để đẩy một Bash invocation xuống background, hoặc prompt Claude chạy background. Output được ghi ra file, Claude đọc lại bằng Read tool. Background task tự dọn khi Claude Code exit; tự chấm dứt nếu output vượt 5GB. Tắt hoàn toàn bằng CLAUDE_CODE_DISABLE_BACKGROUND_TASKS=1.
Hợp cho: build tool (webpack, vite), package manager, test runner, dev server, process dài (docker, terraform).
Side questions với /btw
Hỏi nhanh một câu về công việc hiện tại mà không thêm vào conversation history:
/btw what was the name of that config file again?
- Có full visibility vào conversation hiện tại nhưng không có tool access (chỉ trả lời từ context sẵn có).
- Chạy được cả khi Claude đang làm việc; không interrupt main turn.
- Câu hỏi và câu trả lời là ephemeral — hiện trong overlay dismissible, không vào history.
- Trong overlay:
Space/Enter/Escapeđóng,ccopy Markdown,ffork thành session mới (có full tool access),Left/Rightduyệt các câu/btwcũ.
/btw là nghịch đảo của subagent: nó thấy toàn bộ conversation nhưng không tool; subagent có full tool nhưng context rỗng.
Task list
To-do checklist của Claude cho việc nhiều bước. Bấm Ctrl+T toggle (hiện tối đa 5 task). Khác với background-task view — xem shell/subagent đang chạy dùng /tasks. Task persist qua context compaction. Chia sẻ giữa session bằng CLAUDE_CODE_TASK_LIST_ID=my-project claude.
Prompt suggestions
Khi mở session, một command mờ (grayed-out) gợi ý từ git history. Sau khi Claude phản hồi, suggestion tiếp tục dựa trên conversation. Tab/Right arrow để đặt vào input, gõ bất kỳ để bỏ. Tắt bằng export CLAUDE_CODE_ENABLE_PROMPT_SUGGESTION=false.
Session recap và PR status
- Recap: khi quay lại terminal sau khi rời đi, Claude Code hiện recap một dòng. Sinh ngầm sau ≥3 phút không focus. On-demand:
/recap. - PR status: trên branch có PR mở, footer hiện link kiểu "PR #446" với gạch chân màu — xanh (approved), vàng (pending), đỏ (changes requested), xám (draft). Cần
ghđã cài và auth.Cmd+click/Ctrl+clickmở PR; refresh mỗi 60s.
Transcript viewer và command history
Ctrl+Otoggle transcript viewer — hiện tool usage chi tiết, timestamp, model từng message, và bung MCP call.Ctrl+Rreverse search command history. Input history lưu per working directory, reset khi/clear.
Vim editor mode
Bật qua /config → Editor mode. Hỗ trợ NORMAL/INSERT/VISUAL, motions (w/e/b, f{char}, gg/G), editing (dd, cw, yy, p), text objects (iw, i", i(). Block-wise visual (Ctrl+V) không hỗ trợ. Map chuỗi 2 phím INSERT-mode về Escape bằng vimInsertModeRemaps (vd "jj": "<Esc>", cần v2.1.208+).
3.5 Bảng tra keybinding
General controls
| Shortcut | Tác dụng |
|---|---|
Ctrl+C | Interrupt operation đang chạy; nếu không có gì chạy, lần 1 clear input, lần 2 exit |
Ctrl+D | Exit (bấm 2 lần trong 800ms); khi có text, xóa ký tự sau con trỏ |
Esc | Dừng Claude giữa turn để redirect (giữ work đã làm); đóng dialog nếu đang mở |
Esc + Esc | Có text: clear draft (lưu vào history). Rỗng: mở rewind menu |
Shift+Tab | Cycle permission mode (default → acceptEdits → plan → ...) |
Ctrl+G / Ctrl+X Ctrl+E | Mở prompt trong text editor mặc định |
Ctrl+O | Toggle transcript viewer |
Ctrl+R | Reverse search command history |
Ctrl+V (Cmd+V iTerm2, Alt+V Win/WSL) | Paste image từ clipboard |
Ctrl+B | Background task đang chạy (tmux: 2 lần) |
Ctrl+T | Toggle task checklist của Claude |
Ctrl+S | Stash / restore prompt |
Ctrl+L | Redraw màn hình (giữ input và history) |
Ctrl+Z | Suspend process (Unix); fg để resume |
Ctrl+X Ctrl+K | Dừng mọi background subagent (bấm 2 lần trong 3s để xác nhận) |
Option/Alt+P | Switch model |
Option/Alt+T | Toggle extended thinking |
Option/Alt+O | Toggle fast mode |
Text editing (readline)
| Shortcut | Tác dụng |
|---|---|
Ctrl+A / Ctrl+E | Về đầu / cuối dòng logic hiện tại |
Ctrl+K / Ctrl+U | Xóa tới cuối dòng / từ con trỏ về đầu dòng |
Ctrl+W | Xóa từ trước đó |
Ctrl+Y | Paste text vừa xóa (Ctrl+K/U/W) |
Alt+B / Alt+F | Lùi / tiến một từ |
Ctrl+_ | Undo lần edit input gần nhất |
Multiline input
| Cách | Shortcut | Ghi chú |
|---|---|---|
| Quick escape | \ + Enter | Mọi terminal |
| Option key | Option+Enter | Sau khi bật Option as Meta trên macOS |
| Shift+Enter | Shift+Enter | Native ở iTerm2, WezTerm, Ghostty, Kitty, Warp, Apple Terminal, Windows Terminal |
| Control sequence | Ctrl+J | Mọi terminal, không cần config |
Với VS Code, Cursor, Alacritty, Zed... chạy /terminal-setup để cài binding Shift+Enter.
Quick commands (đầu input)
| Ký tự | Tác dụng |
|---|---|
/ | Command hoặc skill |
! | Shell mode |
@ | File path autocomplete |
? (input rỗng) | Toggle panel trợ giúp shortcut |
macOS: các shortcut
Alt+B/F/Y/Pcần cấu hình Option làm Meta (iTerm2: Profiles → Keys → Left/Right Option = "Esc+"; Apple Terminal: Keyboard → "Use Option as Meta Key"; VS Code:"terminal.integrated.macOptionIsMeta": true).
Customize keybinding
Chạy /keybindings để tạo/mở ~/.claude/keybindings.json. File có mảng bindings, mỗi block gắn một context với map keystroke → action (dạng namespace:action). Thay đổi tự áp dụng, không cần restart.
{
"$schema": "https://www.schemastore.org/claude-code-keybindings.json",
"bindings": [
{
"context": "Chat",
"bindings": {
"ctrl+e": "chat:externalEditor",
"ctrl+u": null
}
}
]
}
- Set action thành
nullđể unbind default. - Reserved, không rebind được:
Ctrl+C,Ctrl+D,Ctrl+M(= Enter),Caps Lock. - Xung đột terminal:
Ctrl+B(tmux prefix),Ctrl+A(GNU screen),Ctrl+Z(SIGTSTP). - Chord là chuỗi keystroke cách nhau bằng space (
ctrl+k ctrl+s). Uppercase đơn ngụ ý Shift (K=shift+k). - Modifier:
meta/alt/opt= Alt trên Win/Linux, Option trên macOS;cmd/super/winchỉ nhận diện ở terminal report Super modifier — dùngctrl/metacho binding chạy mọi nơi. --debugđể xem warning validation (context sai, reserved conflict, duplicate binding).
Xem thêm
content/en/docs/claude-code/common-workflows.md— recipe từng bước cho debug, test, PRcontent/en/docs/claude-code/best-practices.md— pattern quản lý context và scalecontent/en/docs/claude-code/interactive-mode.md— tham chiếu đầy đủ interactive modecontent/en/docs/claude-code/keybindings.md— tham chiếu keybinding và action đầy đủ- Các chương liên quan: plan mode / permission modes, sub-agents, checkpointing, sessions, memory (CLAUDE.md)