กลับไปหน้าสูตร
#gemini#ai#cli#cheatsheet#automation#mcp
Gemini CLI Cheatsheet
คู่มือฉบับสมบูรณ์สำหรับ Gemini CLI: การจัดการ Context, MCP Servers, Agent Skills และการทำ Workflow Automation ขั้นสูง
11 มีนาคม 2569อ่านประมาณ 5 นาที
สารบัญสูตร
🚀 Core CLI Commands (Global)📦 Official Install & Auth Options✅ Official Notes จาก Repo / Docs🌟 จุดเด่นเฉพาะตัวของ Gemini CLI🤖 Subagents & Agent Skills🧩 Custom Commands & Extensions🛠 Interactive Slash Commands (Inside Session)🔗 At Commands (@) & Source Injection🐚 Shell & System Integration (!)🤖 Model Context Protocol (MCP)⌨️ Advanced Keyboard Shortcuts💡 Automation & Recipes📅 Daily Pro Workflow
🚀 Core CLI Commands (Global)
| Command | คำอธิบายเจาะลึก |
|---|---|
gemini | เริ่มต้น Interactive REPL (แนะนำสำหรับการทำงานแบบต่อเนื่อง) |
npx @google/gemini-cli | รันแบบไม่ต้องติดตั้งล่วงหน้า |
gemini "คำถาม" | รันคำสั่งเดียวแล้วจบ (One-off query) เหมาะสำหรับ Pipe output |
gemini -i "คำถาม" | ถามก่อนแล้วเข้าสู่โหมด Interactive ต่อทันที |
gemini -r "latest" | กู้คืน session ล่าสุด (Resume) ไม่ต้องเริ่มใหม่ |
gemini --include-directories ../lib,../docs | เพิ่มหลายโฟลเดอร์เข้า context ตั้งแต่ตอนเริ่ม |
gemini -m gemini-2.5-flash | ระบุโมเดลตั้งแต่ตอนเปิดใช้งาน |
gemini -p "อธิบายโค้ดนี้" --output-format json | non-interactive พร้อมผลลัพธ์ JSON |
gemini -p "Run tests and deploy" --output-format stream-json | stream JSON events สำหรับ automation |
gemini update | ตรวจสอบและอัปเดตเวอร์ชัน (แนะนำให้ทำบ่อยๆ) |
gemini extensions | จัดการส่วนเสริม เช่น list, install, enable, disable |
gemini mcp | ตั้งค่า Model Context Protocol servers เพื่อเชื่อมต่อเครื่องมือภายนอก |
gemini skills | จัดการความสามารถพิเศษ (Skills) ของ Agent |
📦 Official Install & Auth Options
| วิธี | คำสั่ง / ตัวแปร |
|---|---|
| Run instantly | npx @google/gemini-cli |
| npm global | npm install -g @google/gemini-cli |
| Homebrew | brew install gemini-cli |
| Sign in with Google | รัน gemini แล้วเลือก login ผ่าน browser |
| Gemini API key | export GEMINI_API_KEY="YOUR_API_KEY" |
| Vertex AI | export GOOGLE_GENAI_USE_VERTEXAI=true |
✅ Official Notes จาก Repo / Docs
- Gemini CLI มี free tier สำหรับผู้ใช้ Google account ส่วนบุคคล
- รองรับ non-interactive scripting โดยตรงผ่าน
-p - รองรับ
--output-format jsonและstream-jsonสำหรับ automation - รองรับ Google Search grounding, file operations, shell commands และ web fetch
- รองรับ MCP servers และ custom commands อย่างเป็นทางการ
- มี
GEMINI.mdสำหรับ project context โดยตรง
🌟 จุดเด่นเฉพาะตัวของ Gemini CLI
- เด่นเรื่อง
Extensionsอย่างเป็นทางการ ทำให้สามารถแพ็ก capabilities, commands และ integrations เป็นหน่วยแจกจ่ายได้ชัด - มี
browser_agentแบบ built-in ใน docs สำหรับ browser automation ผ่าน accessibility tree โดยตรง ซึ่งไม่ใช่ทุกตัวมีเป็น built-in agent - มีระบบ
@{...},!{...},{{args}}ใน custom commands ชัดและ practical มากสำหรับ prompt templating - รองรับทั้ง Google account sign-in, Gemini API key และ Vertex AI ทำให้เหมาะกับทั้งสายบุคคลและสาย enterprise บน Google Cloud
- มี free tier และเชื่อม ecosystem ของ Google ได้ดี เช่น Search grounding และ tooling ฝั่ง Google
🤖 Subagents & Agent Skills
Built-in Subagents จาก docs
| Subagent | ใช้ทำอะไร |
|---|---|
codebase_investigator | วิเคราะห์ codebase และ trace dependency |
cli_help | ตอบเรื่อง Gemini CLI, config, docs |
generalist_agent | route งานไปหาตัวเชี่ยวชาญอื่น |
browser_agent | ทำ browser automation แบบ experimental |
วิธีเรียก
- automatic delegation: main agent จะเลือกเรียกเองถ้างานตรงกับความเชี่ยวชาญ
- บังคับด้วย
@subagent_nameที่ต้น prompt เช่น@codebase_investigator ... - จัดการผ่าน
/agents
วิธีเรียก Subagent ชัดๆ
| วิธี | ตัวอย่าง |
|---|---|
| ให้ระบบเลือกเอง | ถามเช่น How does the auth system work? แล้ว main agent อาจเรียก codebase_investigator |
บังคับด้วย @ syntax | @codebase_investigator Map out the relationship between AgentRegistry and LocalAgentExecutor. |
| ปรับ config ของ agent | /agents config codebase_investigator |
| ปิด/เปิด agent | /agents disable <name> / /agents enable <name> |
หลักสำคัญจาก docs:
- ถ้าขึ้นต้น prompt ด้วย
@subagent_nameGemini จะใส่ system note เพื่อบังคับ route ไปหาตัวนั้น - subagent มี context และ toolset แยกจาก main agent
- subagent ใน Gemini ไม่สามารถเรียก subagent อื่นต่อซ้อนกันได้
คำสั่ง /agents ที่ docs ระบุ
| Command | ใช้ทำอะไร |
|---|---|
/agents list | ดู built-in, local, และ remote agents |
/agents reload | reload registry ของ agents |
/agents enable <name> | เปิดใช้งาน agent |
/agents disable <name> | ปิด agent |
/agents config <name> | ปรับ model / temperature / execution limits |
Custom Subagent Files
| Scope | Path |
|---|---|
| Project | .gemini/agents/*.md |
| User | ~/.gemini/agents/*.md |
ตัวอย่างจาก docs:
---
name: security-auditor
description: Specialized in finding security vulnerabilities in code.
kind: local
tools:
- read_file
- grep_search
model: gemini-3-flash-preview
temperature: 0.2
max_turns: 10
---
You are a ruthless Security Auditor.
🧩 Custom Commands & Extensions
File Locations
| Scope | Path |
|---|---|
| Global | ~/.gemini/commands/ |
| Project | <project>/.gemini/commands/ |
ชื่อ command
~/.gemini/commands/test.toml->/test<project>/.gemini/commands/git/commit.toml->/git:commit
โครง .toml
description = "Generates a Git commit message based on staged changes."
prompt = """Please generate a Conventional Commit message based on the staged diff below:
!{git diff --staged}
"""
Placeholder / Syntax ที่ docs รองรับ
| Syntax | ความหมาย |
|---|---|
{{args}} | inject arguments แบบ context-aware |
!{...} | รัน shell command แล้ว inject output |
@{...} | inject file content หรือ directory listing |
วิธีเรียก Custom Command ชัดๆ
| วิธี | ตัวอย่าง |
|---|---|
| command ปกติ | /test |
| namespaced command | /git:commit |
| ส่ง arguments | /review FileCommandLoader.ts |
reload หลังแก้ไฟล์ .toml | /commands reload |
Extensions
- Gemini CLI มีระบบ Extensions อย่างเป็นทางการ
- จัดการผ่าน
/extensions - คำสั่งสำคัญ เช่น
install,link,list,enable,disable,update
🛠 Interactive Slash Commands (Inside Session)
📂 Workspace & Context
| Command | การใช้งาน |
|---|---|
/init | สร้าง GEMINI.md สแกนโปรเจกต์เพื่อให้ AI เข้าใจ Architecture |
/directory add <path> | เพิ่มโฟลเดอร์อื่นเข้ามาใน Context (ทำงานข้ามโปรเจกต์ได้) |
/memory add "ข้อความ" | บันทึกความจำระยะยาว (Global Memory) ที่ AI จะจำได้ทุก session |
/memory list | ดูรายการความจำทั้งหมดที่บันทึกไว้ |
/compress | ย่อประวัติการคุย (Summarize) เพื่อลด Token Usage ใน session ยาวๆ |
/rewind | ย้อนกลับไปก่อนการตอบครั้งล่าสุด (Esc 2 ครั้ง) |
⚙️ System & Settings
| Command | การใช้งาน |
|---|---|
/model set <name> | สลับรุ่นโมเดล (เช่น Flash, Pro, Ultra) |
/settings | เปิด Interactive Editor แก้ไขไฟล์ .gemini/settings.json |
/theme | ปรับแต่งสีสัน (Dark, Light, หรือ Custom) |
/tools | แสดงรายการ Tools/Functions ทั้งหมดที่ AI มีสิทธิ์ใช้ |
/permissions trust | อนุญาตให้ AI แก้ไขไฟล์ในโฟลเดอร์นี้ถาวร |
/stats usage | ดู Token consumed, Estimated cost และ Latency |
💾 Session Management
| Command | การใช้งาน |
|---|---|
/chat save <name> | เซฟ session ปัจจุบันเก็บไว้ (Checkpoint) |
/chat list | ดูรายการเซสชันที่เคยเซฟไว้ทั้งหมด |
/chat resume <id> | โหลดเซสชันเก่ากลับมาคุยต่อ |
/chat delete <id> | ลบเซสชันที่ไม่ต้องการ |
🔗 At Commands (@) & Source Injection
การดึงข้อมูลจากแหล่งต่างๆ เข้ามาใน Prompt:
- Local Files:
@src/app.tsx(ระบุไฟล์) หรือ@lib/(ทั้งโฟลเดอร์) - Git Context:
@git:staged(เฉพาะที่แอดไว้),@git:diff(ที่ยังไม่ commit) - Web Content:
@https://github.com/...(ดึงโค้ดจากเว็บ) - Filtered Search:
@src/**/*.ts(ระบุ pattern ของไฟล์) - Clipboard:
@clip(ดึงข้อมูลล่าสุดจาก clipboard)
🐚 Shell & System Integration (!)
| Syntax | การใช้งานขั้นสูง |
|---|---|
!<command> | รันคำสั่งระบบ เช่น !npm run dev |
! | เข้าสู่ "Shell Mode" (พิมพ์คำสั่งได้ยาวๆ เหมือนอยู่ใน terminal ปกติ) |
!! | รันคำสั่ง shell ล่าสุดซ้ำอีกครั้ง |
/shells | เปิดดู Background processes (ถ้าสั่งรัน server ไว้) |
🤖 Model Context Protocol (MCP)
Gemini CLI รองรับ MCP เพื่อขยายขีดความสามารถ:
- เชื่อมต่อ Database: ให้ AI คิวรีข้อมูลจาก SQL/NoSQL ได้โดยตรง
- เชื่อมต่อ API: ดึงข้อมูลจาก Slack, Google Search, Brave Search
- Local Tools: ให้ AI รัน Script ในเครื่องเพื่อจัดการไฟล์ซับซ้อน
# ตัวอย่างการเพิ่ม MCP Server
gemini mcp add sqlite-reader --command "npx" --args "-y, @modelcontextprotocol/server-sqlite"
⌨️ Advanced Keyboard Shortcuts
| Shortcut | Action |
|---|---|
Alt + Enter | ขึ้นบรรทัดใหม่ (Multiline Input) |
Ctrl + C | หยุดการตอบของ AI ทันที (Abort) |
Ctrl + L | ล้างหน้าจอ (Clear Screen) |
Tab | Autocomplete ชื่อไฟล์, คำสั่ง Slash, หรือตัวแปร |
Ctrl + F | โฟกัสไปที่คำสั่ง Shell ที่กำลังรันอยู่ (Interactive Shell) |
💡 Automation & Recipes
1) CI/CD Debugger
gemini "รัน !npm test ถ้า error ให้ดู @logs/error.log แล้วแก้โค้ดใน @src/ ให้ผ่าน"
2) Documentation Generator
gemini "สแกนโค้ดใน @src/ ทั้งหมด แล้วเขียน README.md ให้ละเอียดตามมาตรฐานใน @GEMINI.md"
3) Code Refactor (Step-by-Step)
gemini /plan "ช่วยเปลี่ยน @lib/old-api.ts เป็น @lib/new-api.ts โดยอ้างอิงจาก @docs/api-v2.md"
4) JSON Automation Output
gemini -p "สรุป architecture ของ repo นี้" --output-format json
5) Stream Events สำหรับ long-running job
gemini -p "รัน test และสรุป failure" --output-format stream-json
📅 Daily Pro Workflow
- Check Status:
gemini /statsดูยอด Token คงเหลือ - Sync Context:
/initเพื่ออัปเดตความเข้าใจของ AI ต่อโปรเจกต์ - Work: พัฒนาฟีเจอร์โดยใช้
@fileและรันเทสด้วย!npm test - Review:
/planก่อนเริ่มงานใหญ่ เพื่อลดความผิดพลาด - Save:
/chat save "feature-x-done"เพื่อเก็บเป็นประวัติ