编程笔记

lifelong learning & practice makes perfect

AI|Gemini CLI,自定义斜杠命令(译)

源自 | 2025年7月31日 Jack Wotherspoon, 开发者倡导者 Abhi Patel, 软件工程师

custom_slash_commands_header

今天,我们宣布在 Gemini CLI 中支持自定义斜杠命令!这个备受期待的功能可让您定义可重用的提示,以简化与 Gemini CLI 的交互,并有助于提高跨工作流程的效率。斜杠命令可以在本地 .toml 文件中定义,也可以通过模型上下文协议 (MCP) 提示来定义。准备好利用斜杠命令的全新功能来改变您利用 Gemini CLI 的方式!

custom_slash_commands_review

要使用斜杠命令,请确保您更新到最新版本的 Gemini CLI。

更新 npx:

1
npx @google/gemini-cli

更新 npm:

1
npm install -g @google/gemini-cli@latest

强大的、可扩展的 .toml 文件基础

自定义斜杠命令的基础植根于 .toml 文件。

.toml 文件提供了一个强大且结构化的基础,可在此基础上为复杂命令构建广泛的支持。为了帮助支持广泛的用户,我们将所需的密钥减至最少(只需 prompt)。我们还支持易于使用的参数 {{args}} 和 shell 命令执行 !{...},可直接在提示中使用。

以下是一个示例 .toml 文件,可使用 Gemini CLI 中的 /review <issue_number> 调用以审查 GitHub PR。请注意,文件名定义了命令名称,并且区分大小写。有关自定义斜杠命令的更多信息,请参阅 Gemini CLI 文档的自定义命令部分。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
description="Reviews a pull request based on issue number."
prompt = """
Please provide a detailed pull request review on GitHub issue {{args}}.
Follow these steps:
1. Use `gh pr view {{args}}` to pull the information of the PR.
2. Use `gh pr diff {{args}}` to view the diff of the PR.
3. Understand the intent of the PR using the PR description.
4. If PR description is not detailed enough to understand the intent,
make sure to note it in your review.
5. Make sure the PR title follows Conventional Commits, here are some recent
commits to the repo as examples: !{git log --pretty=format:%s -n 10}
6. Search the codebase if required.
7. Write a concise review of the PR, keeping in mind to encourage high
quality and best practices.
8. Use `gh pr comment {{args}} --body {{review}}` to post the review.
Remember to use the GitHub CLI (`gh`) with the Shell tool for all
GitHub-related tasks.
"""

命名空间

命令的名称由其相对于 commands 目录的文件路径确定。子目录用于创建命名空间命令,路径分隔符(/ 或 \)将转换为冒号 (:)。

  • <project>/.gemini/commands/test.toml 处的文件将成为 /test 命令。
  • <project>/.gemini/commands/git/commit.toml 处的文件将成为命名空间命令 /git:commit

custom_slash_commands_namespaces

这允许将相关命令分组到单个命名空间下。

构建斜杠命令

接下来的几个部分将向您展示如何为 Gemini CLI 构建斜杠命令。

1 - 创建命令文件

首先,在 ~/.gemini/commands/ 目录中创建一个名为 plan.toml 的文件。这样做会让您创建一个 /plan 命令,告诉 Gemini CLI 仅通过提供分步计划来规划更改,而不开始实施。这种方法可让您在实施前提供反馈并迭代计划。

自定义斜杠命令可以通过在指定目录中定义 .toml 文件来限定于单个用户或项目。

  • 用户范围的命令可用于用户的所有 Gemini CLI 项目,并存储在 ~/.gemini/commands/ 中(注意 ~)。
  • 项目范围的命令仅可从给定项目中的会话中获得,并存储在 .gemini/commands/ 中。

提示:要简化项目工作流程,请将这些文件签入 Git 存储库!

1
2
mkdir -p ~/.gemini/commands
touch ~/.gemini/commands/plan.toml

2 - 添加命令定义

打开 plan.toml 并添加以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# ~/.gemini/commands/plan.toml
description="Investigates and creates a strategic plan to accomplish a goal."
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.
You MUST NOT write, modify, or execute any code. Your sole focus is on the plan.
Use your available "read" and "search" tools to research and gather information.
Present your strategic plan in markdown. It should be the direct and only output.
1. **Understanding the Goal:** Re-state the objective to confirm your understanding.
2. **Investigation & Analysis:** Describe the investigative steps you will take.
3. **Proposed Strategic Approach:** Outline the high-level strategy.
4. **Verification Strategy:** Explain how the success of the plan will be verified.
5. **Anticipated Challenges & Considerations:** Based on your analysis, list potential issues.
Your final output should be ONLY this strategic plan.
"""

3 - 使用命令

现在您可以在 Gemini CLI 中使用此命令:

1
/plan How can I make the project more performant?

Gemini 将规划出更改并输出详细的分步执行计划!

与 MCP 提示的丰富集成

Gemini CLI 现在通过支持 MCP 提示 作为斜杠命令,提供了与 MCP 更集成的体验!MCP 为服务器向客户端公开提示模板提供了一种标准化的方法。Gemini CLI 利用这一点来公开已配置 MCP 服务器的可用提示,并使这些提示可用作斜杠命令。

MCP 提示的名称和描述将用作斜杠命令的名称和描述。还支持 MCP 提示参数,并通过使用 /mycommand --<argument_name>="<argument_value>" 或按位置 /mycommand <argument1> <argument2> 在斜杠命令中加以利用。

以下是使用 FastMCP Python 服务器的 /research 命令示例:

custom_slash_commands_mcp

轻松入门

还在等什么?立即使用 Gemini CLI 升级您的终端体验,并试用自定义斜杠命令来简化您的工作流程。要了解更多信息,请查看 Gemini CLI 的自定义命令文档。

原文

欢迎关注我的其它发布渠道