编程笔记

lifelong learning & practice makes perfect

AI|Gemini CLI,Custom slash commands(转载)

July 31, 2025

Jack Wotherspoon, Developer Advocate

Abhi Patel, Software Engineer

custom_slash_commands_header

Today, we’re announcing support for custom slash commands in Gemini CLI! This highly requested feature lets you define reusable prompts for streamlining interactions with Gemini CLI and helps improve efficiency across workflows. Slash commands can be defined in local .toml files or through Model Context Protocol (MCP) prompts. Get ready to transform how you leverage Gemini CLI with the new power of slash commands!

custom_slash_commands_review

To use slash commands, make sure that you update to the latest version of Gemini CLI.

Update npx:

1
npx @google/gemini-cli

Update npm:

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

Powerful and extensible foundation with .toml files

The foundation of custom slash commands is rooted in .toml files.

The .toml file provides a powerful and structured base on which to build extensive support for complex commands. To help support a wide range of users, we made the required keys minimal (just prompt). And we support easy-to-use args with {{args}} and shell command execution !{...} directly into the prompt.

Here is an example .toml file that is invoked using /review <issue_number> from Gemini CLI to review a GitHub PR. Notice that the file name defines the command name and it’s case sensitive. For more information about custom slash commands, see the Custom Commands section of the Gemini CLI documentation.

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.
"""

Namespacing

The name of a command is determined by its file path relative to the commands directory. Sub-directories are used to create namespaced commands, with the path separator (/ or ) being converted to a colon (:).

  • A file at <project>/.gemini/commands/test.toml becomes the command /test.
  • A file at <project>/.gemini/commands/git/commit.toml becomes the namespaced command /git:commit.

custom_slash_commands_namespaces

This allows grouping related commands under a single namespace.

Building a slash command

The next few sections show you how to build a slash command for Gemini CLI.

1 - Create the command file

First, create a file named plan.toml inside the ~/.gemini/commands/ directory. Doing so will let you create a /plan command to tell Gemini CLI to only plan the changes by providing a step-by-step plan and to not start on implementation. This approach will let you provide feedback and iterate on the plan before implementation.

Custom slash commands can be scoped to an individual user or project by defining the .toml files in designated directories.

  • User-scoped commands are available across all Gemini CLI projects for a user and are stored in ~/.gemini/commands/ (note the ~).
  • Project-scoped commands are only available from sessions within a given project and are stored in .gemini/commands/.

Hint: To streamline project workflows, check these into Git repositories!

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

2 - Add the command definition

Open plan.toml and add the following content:

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 - Use the command

Now you can use this command within Gemini CLI:

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

Gemini will plan out the changes and output a detailed step-by-step execution plan!

Enriched integration with MCP Prompts

Gemini CLI now offers a more integrated experience with MCP by supporting MCP Prompts as slash commands! MCP provides a standardized way for servers to expose prompt templates to clients. Gemini CLI utilizes this to expose available prompts for configured MCP servers and make the prompts available as slash commands.

The name and description of the MCP prompt is used as the slash command name and description. MCP prompt arguments are also supported and leveraged in slash commands by using /mycommand --<argument_name>="<argument_value>" or positionally /mycommand <argument1> <argument2>.

The following is an example /research command that uses FastMCP Python server:

custom_slash_commands_mcp

Easy to get started

So what are you waiting for? Upgrade your terminal experience with Gemini CLI today and try out custom slash commands to streamline your workflows. To learn more, check out the Custom Commands documentation for the Gemini CLI.

原文

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