Gemini CLI:自定义斜杠命令

概述

Gemini CLI 在 7 月 31 日的更新中引入了一项强大的功能:自定义斜杠命令。这允许用户创建可复用的、带有参数的提示词,从而极大地简化常见任务并提高工作效率。一句话解释,就是用 TOML 将 Prompt 和 Shell 结合起来,并通过斜杠命令(Slash Command)简化调用。

原文链接: Gemini CLI: Custom slash commands

简单总结一下:

  1. Prompt 模板化:您把常用的、复杂的 prompt 写在 TOML 文件里,形成一个可复用的模板。
  2. Shell 能力注入 :通过 !{...} 语法,您可以将 shell 命令的执行结果动态嵌入到 prompt 模板中,让 prompt 能获取本地文件的内容或系统信息。
  3. 斜杠命令调用:Gemini CLI 将这些 TOML 文件变成了简单易用的斜杠命令,让您用简短的命令触发复杂的、包含动态本地上下文的 prompt。

核心概念

1. 配置文件格式

自定义命令通过 TOML (.toml) 文件进行定义。这是目前唯一支持的格式。

2. 配置文件位置

Gemini CLI 会在以下两个位置查找命令配置文件:

  • 项目级: 在您项目的根目录下的 .gemini/commands/ 目录中。这允许您为特定项目定义命令。
  • 用户级: 在您用户主目录下的 ~/.gemini/commands/ 目录中。在这里定义的命令在所有项目中都可用。

3. 命令的定义

  • 文件名即命令名: 文件的名称(不含 .toml 后缀)决定了斜杠命令的名称。例如,plan.toml 文件会创建一个名为 /plan 的命令。
  • prompt 键: 每个命令文件都必须包含一个 prompt 键,其值就是您希望执行的提示词内容。

4. 在提示词中使用变量和 Shell 命令

  • 用户参数 ({{args}}) : 您可以在 prompt 中使用 {{args}} 占位符,它会被替换为用户在斜杠命令后输入的任何文本。
  • Shell 命令 (!{...}): 您可以通过 !{...} 语法在提示词中嵌入任何 shell 命令的输出。这为与文件系统或其他工具交互提供了极大的灵活性。

5. 使用命名空间

您可以通过在 commands 目录下创建子目录来组织您的命令。例如,一个位于 .gemini/commands/git/commit.toml 的配置文件将创建一个名为 /git:commit 的命令。

6. 关于 """

在 TOML 文件中,您可以使用三重引号 """ 来定义多行字符串。这对于编写长提示词非常有用。类似于js的字符串模板`

实践示例

示例 1: 创建一个规划命令

假设您经常需要 Gemini 为某个任务制定计划而不是直接生成代码。

  1. 创建文件:
bash 复制代码
mkdir -p .gemini/commands
touch .gemini/commands/plan.toml
  1. 编辑文件内容:
toml 复制代码
# .gemini/commands/plan.toml

prompt = """
You are a world-class software engineer.
I want you to act as a thought-partner.
Do not generate code.
I am going to ask you a question, and I want you to describe a plan to answer it.

My question is: {{args}}
"""
  1. 使用命令:
bash 复制代码
/plan "Refactor the authentication module"

Gemini 将会收到被包裹后的提示词,并为您提供重构计划。

示例 2: 创建一个读取文件的命令

这个例子展示了如何结合 shell 命令来创建一个动态读取文件内容的命令。

  1. 创建文件:
bash 复制代码
touch .gemini/commands/file.toml
  1. 编辑文件内容:
toml 复制代码
# .gemini/commands/file.toml

prompt = """
I am going to ask you a question about the following file.
File path: {{args}}
File content:

!{cat {{args}}}

"""
  1. 使用命令:
bash 复制代码
/file "src/components/Button.tsx"

这会读取 src/components/Button.tsx 的内容,并将其作为上下文提供给 Gemini。

我的自定义命令

自动生成代码提交信息

toml 复制代码
# 文件路径: .gemini/commands/commit.toml
# 指令名称: /commit

description = "自动暂存所有更改,并根据 diff 内容生成一个完整的、符合规范的 git commit 命令。"

prompt = """
请严格扮演一个专业的Git提交信息生成器,并遵循 Conventional Commits v1.0.0 规范。
任务是根据以下 `git diff --staged` 的内容,生成一个**完整并且可以直接执行**的 `git commit -m \"...\"` 命令。

Staged Diff 内容:
!{git add -A && git diff --staged}

请遵循以下步骤分析 Diff 并构建提交信息:
1.  **类型(Type)**: 从 `feat`, `fix`, `refactor`, `chore`, `docs`, `style`, `test`, `perf` 中选择最合适的类型。
2.  **范围(Scope)**: 提供一个简短的名词来描述代码中受影响的部分 (例如: `api`, `db`, `ui`)。这是可选的。
3.  **描述(Description)**: 撰写一个简短、清晰的摘要,必须使用现在时祈使句 (例如: 使用 \"add\" 而不是 \"added\" 或 \"adds\")。

最后,将上述内容组合成一个符合 `type(scope): description` 格式的提交信息,并将其包装在一个完整的 shell 命令中。

**最终输出要求:**
- 最终输出**必须**是一个可以直接复制并执行的、单行的 `git commit` 命令。
- **不要**包含任何额外的解释、前言或结尾。
- 如果 diff 内容为空,则输出 `echo \"没有检测到任何更改需要提交。\"`。

请直接输出最终的命令。
"""

plan 模式

toml 复制代码
# ~/.gemini/commands/plan.toml

description="Investigates and creates a strategic plan to accomplish a task."
prompt = """
Your primary role is that of a strategist, not an implementer.
Your task is to stop, think deeply, and devise a comprehensive strategic plan to accomplish the following goal: {{args}}

You MUST NOT write, modify, or execute any code. Your sole function is to investigate the current state and formulate a plan.

Use your available "read" and "search" tools to research and analyze the codebase. Gather all necessary context before presenting your strategy.

Present your strategic plan in markdown. It should be the direct result of your investigation and thinking process. Structure your response with the following sections:

1.  **Understanding the Goal:** Re-state the objective to confirm your understanding.
2.  **Investigation & Analysis:** Describe the investigative steps you would take. What files would you need to read? What would you search for? What critical questions need to be answered before any work begins?
3.  **Proposed Strategic Approach:** Outline the high-level strategy. Break the approach down into logical phases and describe the work that should happen in each.
4.  **Verification Strategy:** Explain how the success of this plan would be measured. What should be tested to ensure the goal is met without introducing regressions?
5.  **Anticipated Challenges & Considerations:** Based on your analysis, what potential risks, dependencies, or trade-offs do you foresee?

Your final output should be ONLY this strategic plan.
"""
相关推荐
一缕82年的清风18 小时前
智能体“自适应慢思考”的算力黑洞:当 Claude 5.5 与 GPT-6.1 Sol 陷入推理自旋,为什么高并发架构必须重构“认知预算熔断与快慢推测执行”?
gpt·claude·gemini
星核0penstarry3 天前
视频理解模型选型指南:Gemini、Qwen、SeedRealtime 怎么选(附 API 示例)
人工智能·音视频·实时音视频·视频·gemini·vs qwen
一缕82年的清风6 天前
谷歌突发王炸:Gemini 4 Carbon 登顶全球第一,大模型三强决战彻底变天
gpt·claude·gemini
sbjdhjd9 天前
AI产业周度动态:从“更强模型”走向“可执行、可治理、可计价”的系统竞争 (2026年10月19日—10月25日)
人工智能·笔记·ai·开源·gemini·信息差·claude code
死磕音视频15 天前
Gemini in Chrome 入门:让浏览器里的 AI 读懂当前网页
chrome·gemini·ai浏览器·网页总结
ClouGence15 天前
最全AI电商生图工具测评:豆包、千问、ChatGPT、WorkBuddy、Nano Banana、即梦、美图设计室、阿可AI
chatgpt·aigc·gemini
孙琦Ray16 天前
西班牙团队开源 AI 评分平台 EvaluAI:加评分标准反而更不准,但更稳定
大语言模型·开源项目·gemini·教育科技·ai 评分·evaluai·apache-2.0
Frag0ut18 天前
Chrome 2026 AI 能力全解析:从 Auto Browse 到 Skills,浏览器正在变成智能代理
chrome·ai·谷歌浏览器·gemini·智能代理·skills·auto browse
死磕音视频18 天前
Gemini 学习功能入门:用引导学习与音频概览把资料听懂看懂
gemini·ai学习·guided learning·audio overview
Web3&Basketball18 天前
实测Gemini4Argon:ERQA 87%登顶长程任务榜
数据库·python·大模型·多模态·gemini·长上下文·推理优化