Gemini CLI支持自定义斜杠命令。Gemini CLI 中的自定义命令允许您将常用的提示保存并作为个人快捷方式重复使用。这些命令可以是全局的(在所有项目中可用),也可以是本地的(特定于一个项目)。
Gemini CLI的斜杠命令可以在本地.toml文件中定义,也可以通过模型上下文协议 (MCP) 提示定义。
在.toml文件中定义斜杠命令
文件位置和优先级
Gemini CLI 按特定顺序从两个位置发现命令:
- 用户命令(全局):位于
~/.gemini/commands/。这些命令在任何项目中都可用。 - 项目命令(本地):位于
<project>/.gemini/commands/。这些是项目特定的,可以commit到版本控制中。
如果项目命令与用户命令同名,则始终使用项目命令。
命名和命名空间
命令的名称源自其相对于 commands 目录的文件路径。子目录创建命名空间化的命令,路径分隔符(/ 或 \)会转换成冒号(:)。
- 位于
<project>/.gemini/commands/test.toml的文件将成为/test命令。 - 位于
~/.gemini/commands/git/commit.toml的文件将成为命名空间化的/git:commit命令。
这允许将相关命令分组到单个命名空间下。
.toml文件格式
命令定义必须在 .toml 文件中。
- 必填字段:
prompt(String): 运行命令时发送给模型的提示。
- 可选字段:
description(String): 显示在/help菜单中的简短单行描述。
处理参数
在自定义命令中处理参数有三种方法:
使用
{{args}}进行简写注入:如果prompt字符串包含{{args}},它将被用户在命令名称后输入的所有文本替换。- 示例 (
git/fix.toml):# In: ~/.gemini/commands/git/fix.toml # Invoked via: /git:fix "Button is misaligned on mobile" description = "Generates a fix for a given GitHub issue." prompt = "Please analyze the staged git changes and provide a code fix for the issue described here: {{args}}." - 运行
/git:fix "Button is misaligned on mobile"会发送提示,其中{{args}}被"Button is misaligned on mobile"替换。
- 示例 (
默认参数处理:如果
prompt不包含{{args}},并且用户提供了参数,则他们键入的整个命令将附加到提示的末尾(在新行上)。这使模型能够看到原始指令和特定参数。如果没有给出参数,则按原样发送提示。使用
!{...}执行 Shell 命令:可以在prompt中执行 shell 命令并注入其输出。CLI 在运行 shell 命令之前会提示确认。示例 (
git/commit.toml):# In: <project>/.gemini/commands/git/commit.toml # Invoked via: /git:commit description = "Generates a Git commit message based on staged changes." # The prompt uses !{...} to execute the command and inject its output. prompt = """ Please generate a Conventional Commit message based on the following git diff: !{git diff --staged} """当运行
/git:commit时,会首先执行git diff --staged,其输出会替换!{...}块,然后将提示发送到模型。
通过MCP提示集成
Gemini CLI 支持MCP 提示作为斜杠命令。
MCP 提示的名称和描述用作斜杠命令名称和描述。MCP 提示参数也被支持,并通过使用 /mycommand --<argument_name>="<argument_value>" 或按位置 /mycommand <argument1> <argument2> 在斜杠命令中利用。
以下是使用 FastMCP Python MCP Server的 research 示例:
main.py:
from fastmcp import FastMCP
mcp = FastMCP("MCP Prompt Server")
@mcp.prompt()
def research(topic: str) -> str:
"""Research and explain a topic."""
return f"Can you please research and explain the topic of {topic} in detail?"
def main():
mcp.run(transport="stdio")
if __name__ == "__main__":
main()~/.gemini/settings.json:
"mcpServers": {
"prompt-server": {
"cwd": "<thepath>/research",
"command": "uv",
"args": ["run", "main.py"]
}
}gemini cli中的集成效果如下:
gemini
Data collection is disabled.
Loaded cached credentials.
╭──────────╮
│ > /mcp │
╰──────────╯
ℹ Configured MCP servers:
🟢 prompt-server - Ready (1 prompt)
Prompts:
- research
No tools available
💡 Tips:
• Use /mcp desc to show server and tool descriptions
• Use /mcp schema to show tool parameter schemas
• Use /mcp nodesc to hide descriptions
• Use /mcp auth <server-name> to authenticate with OAuth-enabled servers
• Press Ctrl+T to toggle tool descriptions on/off
Using: 1 GEMINI.md file | 1 MCP server (ctrl+t to view)
╭────────────────────────────────────────────────╮
│ > /researc │
╰─────────────────────── ────────────────────────╯
research Research and explain a topic.