Home Explore Blog CI



zed

3rd chunk of `docs/src/tasks.md`
f65e43dfa106a05f63a96fa0ca51aff2ed64ad346b1c2cb30000000100000844
- `ZED_SYMBOL`: currently selected symbol; should match the last symbol shown in a symbol breadcrumb (e.g. `mod tests > fn test_task_contexts`)
- `ZED_SELECTED_TEXT`: currently selected text
- `ZED_WORKTREE_ROOT`: absolute path to the root of the current worktree. (e.g. `/Users/my-user/path/to/project`)
- `ZED_CUSTOM_RUST_PACKAGE`: (Rust-specific) name of the parent package of $ZED_FILE source file.

To use a variable in a task, prefix it with a dollar sign (`$`):

```json
{
  "label": "echo current file's path",
  "command": "echo $ZED_FILE"
}
```

You can also use verbose syntax that allows specifying a default if a given variable is not available: `${ZED_FILE:default_value}`

These environmental variables can also be used in tasks' `cwd`, `args`, and `label` fields.

### Variable Quoting

When working with paths containing spaces or other special characters, please ensure variables are properly escaped.

For example, instead of this (which will fail if the path has a space):

```json
{
  "label": "stat current file",
  "command": "stat $ZED_FILE"
}
```

Provide the following:

```json
{
  "label": "stat current file",
  "command": "stat",
  "args": ["$ZED_FILE"]
}
```

Or explicitly include escaped quotes like so:

```json
{
  "label": "stat current file",
  "command": "stat \"$ZED_FILE\""
}
```

## Oneshot tasks

The same task modal opened via `task: spawn` supports arbitrary bash-like command execution: type a command inside the modal text field, and use `opt-enter` to spawn it.

The task modal persists these ad-hoc commands for the duration of the session, `task: rerun` will also rerun such tasks if they were the last ones spawned.

You can also adjust the currently selected task in a modal (`tab` is the default key binding). Doing so will put its command into a prompt that can then be edited & spawned as a oneshot task.

### Ephemeral tasks

You can use the `cmd` modifier when spawning a task via a modal; tasks spawned this way will not have their usage count increased (thus, they will not be respawned with `task: rerun` and they won't have a high rank in the task modal).

Title: Variable Quoting and Oneshot Tasks in Zed
Summary
This section discusses variable quoting in Zed tasks, emphasizing the importance of escaping variables, especially paths with spaces, to prevent command failures. It provides examples of using the `args` field or explicit escaped quotes for proper variable handling. The section also introduces 'oneshot tasks,' which are arbitrary commands executed via the task modal (`task: spawn`). These commands are persisted for the session and can be rerun. The task modal can be used to adjust and edit commands before spawning them as oneshot tasks. Additionally, 'ephemeral tasks,' spawned with the `cmd` modifier, do not increase usage count and are not respawned with `task: rerun`.