The Practical Guide to AI Slash Commands

Good prompts are the difference between great and mediocre AI results. As a result, when you craft a prompt that works well, you'll want to save it so you can reuse it.

Most of you have a collection of prompts that you keep reusing. And you know that manually copying and pasting the same prompts over and over again is slow and time consuming.

The solution is "slash commands".

💡
Slash commands let you define custom commands for repeatable processes. Instead of copying and pasting a prompt, you can invoke it with a slash command - /my-command.

AI tools come with default slash commands like /review, /model, and others. Each tool has its specific set of default commands, but they also let you create custom ones.

Let's say you have a custom prompt for reviewing code:

---
description: "Review the code and provide a detailed analysis of the code."
---

You are performing a code review on the changes in the current branch. The target branch is **origin/main**.

## Code Review Instructions

When reviewing the diff:

1. **Focus on logic and correctness** - Check for bugs, edge cases, and potential issues.
2. **Consider readability** - Is the code clear and maintainable? Does it follow best practices in this repository?
3. **Evaluate performance** - Are there obvious performance concerns or optimizations that could be made?
4. **Assess test coverage** - Does the repository have testing patterns? If so, are there adequate tests for these changes?
5. **Ask clarifying questions** - Ask the user for clarification if you are unsure about the changes or need more context.
6. **Don't be overly pedantic** - Nitpicks are fine, but only if they are relevant issues within reason.

In your output:

- Provide a summary overview of the general code quality.
- Present the identified issues in a table with the columns: index (1, 2, etc.), line number(s), code, issue, and potential solution(s).
- If no issues are found, briefly state that the code meets best practices.

## Getting the Diff

Use the `git diff` tool to fetch the diff.

## Coderabbit review

Get help with reviewing the code by running the command coderabbit --prompt-only. Don't blindly return the review from Coderabbit. Check its accuracy and usefulness and then return the review result.

It's quite a complex prompt. Without slash commands, you need to:

  1. find the file where it's stored
  2. open it
  3. copy the prompt
  4. go back to the AI tool
  5. paste the prompt

All of that can be avoided by creating a custom command /review-code.

You can place the prompt in an markdown file and simply reference its name (/review-code) every time you want to trigger an AI code review.

Caveat: AI tools load the whole file contents into the context window. It's important to keep this in mind since a large context degrades the agent's performance.

Slash Command Format

First of all, these tools map the file name to the command name. If the file name is fix-pr-comments.md, the command is /fix-pr-comments.

Secondly, the prompt's file has the following structure:

  1. Frontmatter metadata that contains:
    1. description: what the command does.
    2. argument-hint: expected parameters. If you use the /fix-pr-comments command, you should pass the pull request number so the agent can fetch the comments from that specific PR.
    3. allowed-tools: which tools the agent can run without asking for the user's permission. For example: Bash(npm install), Git*, etc.
    4. model: the specific model to use for the task.
  2. The prompt itself
💡
The metadata can vary between tools. Check the documentation for the tool you're using.

When to use Slash Commands:

  • You need to manually trigger a workflow.
  • The task is simple and repetitive.
  • You keep copying and pasting the same prompt(s).

Slash Commands Location

Claude Code:

  • Global/user slash commands: ~/.claude/commands
  • Project slash commands: ~/project/.claude/commands

Cursor:

  • Global/user slash commands: ~/.cursor/commands
  • Project slash commands: /project/.cursor/commands

Gemini:

  • Global/user slash commands: ~/.gemini/commands
  • Project slash commands: ~/project/.gemini/commands
Note: Gemini uses .toml instead of Markdown.

Codex:

  • Global/user prompts: ~/.codex/prompts
  • Project prompts: ~/project/.codex/prompts

OpenCode:

  • Global/user commands: ~/.config/opencode/commands/
  • Project commands: .opencode/commands/
⚠️
Some AI tools deprecated (Codex) custom slash commands or merged (Claude) them into skills. However, other tools still use them, so it's still useful to be familiar with them.