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

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 jsonnon-interactive พร้อมผลลัพธ์ JSON
gemini -p "Run tests and deploy" --output-format stream-jsonstream 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 instantlynpx @google/gemini-cli
npm globalnpm install -g @google/gemini-cli
Homebrewbrew install gemini-cli
Sign in with Googleรัน gemini แล้วเลือก login ผ่าน browser
Gemini API keyexport GEMINI_API_KEY="YOUR_API_KEY"
Vertex AIexport 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_agentroute งานไปหาตัวเชี่ยวชาญอื่น
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_name Gemini จะใส่ system note เพื่อบังคับ route ไปหาตัวนั้น
  • subagent มี context และ toolset แยกจาก main agent
  • subagent ใน Gemini ไม่สามารถเรียก subagent อื่นต่อซ้อนกันได้

คำสั่ง /agents ที่ docs ระบุ

Commandใช้ทำอะไร
/agents listดู built-in, local, และ remote agents
/agents reloadreload registry ของ agents
/agents enable <name>เปิดใช้งาน agent
/agents disable <name>ปิด agent
/agents config <name>ปรับ model / temperature / execution limits

Custom Subagent Files

ScopePath
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

ScopePath
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

ShortcutAction
Alt + Enterขึ้นบรรทัดใหม่ (Multiline Input)
Ctrl + Cหยุดการตอบของ AI ทันที (Abort)
Ctrl + Lล้างหน้าจอ (Clear Screen)
TabAutocomplete ชื่อไฟล์, คำสั่ง 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

  1. Check Status: gemini /stats ดูยอด Token คงเหลือ
  2. Sync Context: /init เพื่ออัปเดตความเข้าใจของ AI ต่อโปรเจกต์
  3. Work: พัฒนาฟีเจอร์โดยใช้ @file และรันเทสด้วย !npm test
  4. Review: /plan ก่อนเริ่มงานใหญ่ เพื่อลดความผิดพลาด
  5. Save: /chat save "feature-x-done" เพื่อเก็บเป็นประวัติ