slash-commands.md

  1---
  2title: Slash Commands
  3description: "Slash Commands for Zed extensions."
  4---
  5
  6# Slash Commands
  7
  8Extensions may provide slash commands for use in the Assistant.
  9
 10## Example extension
 11
 12To see a working example of an extension that provides slash commands, check out the [`slash-commands-example` extension](https://github.com/zed-industries/zed/tree/main/extensions/slash-commands-example).
 13
 14This extension can be [installed as a dev extension](./developing-extensions.md#developing-an-extension-locally) if you want to try it out for yourself.
 15
 16## Defining slash commands
 17
 18A given extension may provide one or more slash commands. Each slash command must be registered in the `extension.toml`.
 19
 20For example, here is an extension that provides two slash commands: `/echo` and `/pick-one`:
 21
 22```toml
 23[slash_commands.echo]
 24description = "echoes the provided input"
 25requires_argument = true
 26
 27[slash_commands.pick-one]
 28description = "pick one of three options"
 29requires_argument = true
 30```
 31
 32Each slash command may define the following properties:
 33
 34- `description`: A description of the slash command that will be shown when completing available commands.
 35- `requires_argument`: Indicates whether a slash command requires at least one argument to run.
 36
 37## Implementing slash command behavior
 38
 39To implement behavior for your slash commands, implement `run_slash_command` for your extension.
 40
 41This method accepts the slash command that will be run, the list of arguments passed to it, and an optional `Worktree`.
 42
 43This method returns `SlashCommandOutput`, which contains the textual output of the command in the `text` field. The output may also define `SlashCommandOutputSection`s that contain ranges into the output. These sections are then rendered as creases in the Assistant's context editor.
 44
 45Your extension should `match` on the command name (without the leading `/`) and then execute behavior accordingly:
 46
 47```rs
 48impl zed::Extension for MyExtension {
 49    fn run_slash_command(
 50        &self,
 51        command: SlashCommand,
 52        args: Vec<String>,
 53        _worktree: Option<&Worktree>,
 54    ) -> Result<SlashCommandOutput, String> {
 55        match command.name.as_str() {
 56            "echo" => {
 57                if args.is_empty() {
 58                    return Err("nothing to echo".to_string());
 59                }
 60
 61                let text = args.join(" ");
 62
 63                Ok(SlashCommandOutput {
 64                    sections: vec![SlashCommandOutputSection {
 65                        range: (0..text.len()).into(),
 66                        label: "Echo".to_string(),
 67                    }],
 68                    text,
 69                })
 70            }
 71            "pick-one" => {
 72                let Some(selection) = args.first() else {
 73                    return Err("no option selected".to_string());
 74                };
 75
 76                match selection.as_str() {
 77                    "option-1" | "option-2" | "option-3" => {}
 78                    invalid_option => {
 79                        return Err(format!("{invalid_option} is not a valid option"));
 80                    }
 81                }
 82
 83                let text = format!("You chose {selection}.");
 84
 85                Ok(SlashCommandOutput {
 86                    sections: vec![SlashCommandOutputSection {
 87                        range: (0..text.len()).into(),
 88                        label: format!("Pick One: {selection}"),
 89                    }],
 90                    text,
 91                })
 92            }
 93            command => Err(format!("unknown slash command: \"{command}\"")),
 94        }
 95    }
 96}
 97```
 98
 99## Auto-completing slash command arguments
100
101For slash commands that have arguments, you may also choose to implement `complete_slash_command_argument` to provide completions for your slash commands.
102
103This method accepts the slash command that will be run and the list of arguments passed to it. It returns a list of `SlashCommandArgumentCompletion`s that will be shown in the completion menu.
104
105A `SlashCommandArgumentCompletion` consists of the following properties:
106
107- `label`: The label that will be shown in the completion menu.
108- `new_text`: The text that will be inserted when the completion is accepted.
109- `run_command`: Whether the slash command will be run when the completion is accepted.
110
111Once again, your extension should `match` on the command name (without the leading `/`) and return the desired argument completions:
112
113```rs
114impl zed::Extension for MyExtension {
115    fn complete_slash_command_argument(
116        &self,
117        command: SlashCommand,
118        _args: Vec<String>,
119    ) -> Result<Vec<SlashCommandArgumentCompletion>, String> {
120        match command.name.as_str() {
121            "echo" => Ok(vec![]),
122            "pick-one" => Ok(vec![
123                SlashCommandArgumentCompletion {
124                    label: "Option One".to_string(),
125                    new_text: "option-1".to_string(),
126                    run_command: true,
127                },
128                SlashCommandArgumentCompletion {
129                    label: "Option Two".to_string(),
130                    new_text: "option-2".to_string(),
131                    run_command: true,
132                },
133                SlashCommandArgumentCompletion {
134                    label: "Option Three".to_string(),
135                    new_text: "option-3".to_string(),
136                    run_command: true,
137                },
138            ]),
139            command => Err(format!("unknown slash command: \"{command}\"")),
140        }
141    }
142}
143```