任务

Zed 支持使用其集成的终端来生成(和重新运行)命令以输出结果的方式。 这些命令可以读取 Zed 状态的一个有限子集(例如,当前正在编辑的文件的路径或选定的文本)。

[
  {
    "label": "Example task",
    "command": "for i in {1..5}; do echo \"Hello $i/5\"; sleep 1; done",
    //"args": [],
    // Env overrides for the command, will be appended to the terminal's environment from the settings.
    "env": { "foo": "bar" },
    // Current working directory to spawn the command into, defaults to current project root.
    //"cwd": "/path/to/working/directory",
    // Whether to use a new terminal tab or reuse the existing one to spawn the process, defaults to `false`.
    "use_new_terminal": false,
    // Whether to allow multiple instances of the same task to be run, or rather wait for the existing ones to finish, defaults to `false`.
    "allow_concurrent_runs": false,
    // What to do with the terminal pane and tab, after the command was started:
    // * `always` — always show the task's pane, and focus the corresponding tab in it (default)
    // * `no_focus` — always show the task's pane, add the task's tab in it, but don't focus it
    // * `never` — do not alter focus, but still add/reuse the task's tab in its pane
    "reveal": "always",
    // What to do with the terminal pane and tab, after the command has finished:
    // * `never` — Do nothing when the command finishes (default)
    // * `always` — always hide the terminal tab, hide the pane also if it was the last tab in it
    // * `on_success` — hide the terminal tab on task success only, otherwise behaves similar to `always`
    "hide": "never",
    // Which shell to use when running a task inside the terminal.
    // May take 3 values:
    // 1. (default) Use the system's default terminal configuration in /etc/passwd
    //      "shell": "system"
    // 2. A program:
    //      "shell": {
    //        "program": "sh"
    //      }
    // 3. A program with arguments:
    //     "shell": {
    //         "with_arguments": {
    //           "program": "/bin/bash",
    //           "args": ["--login"]
    //         }
    //     }
    "shell": "system",
    // Whether to show the task line in the output of the spawned task, defaults to `true`.
    "show_summary": true,
    // Whether to show the command line in the output of the spawned task, defaults to `true`.
    "show_output": true,
    // Represents the tags for inline runnable indicators, or spawning multiple tasks at once.
    "tags": []
  }
]

有两个操作可以驱动使用任务的工作流程:task: spawntask: reruntask: spawn 会打开一个模态框,其中包含当前文件中的所有可用任务。 task: rerun 会重新运行最近生成的任务。 您还可以从任务模态框中重新运行任务。

默认情况下,重新运行任务会重用同一个终端(由于 "use_new_terminal": false 的默认值),但会等待之前的任务完成后再启动(由于 "allow_concurrent_runs": false 的默认值)。

保留 "use_new_terminal": false 并设置 "allow_concurrent_runs": true 允许在重新运行时取消之前的任务。

任务模板

可以定义任务

  • 在全局 tasks.json 文件中;此类任务在您处理的所有 Zed 项目中都可用。 此文件通常位于 ~/.config/zed/tasks.json 中。 您可以使用 zed: open tasks 操作来编辑它们。
  • 在特定于工作树的(本地).zed/tasks.json 文件中;此类任务仅在处理包含该工作树的项目时可用。 您可以使用 zed: open project tasks 操作来编辑特定于工作树的任务。
  • 通过 临时任务 动态创建。 这些任务是特定于项目的,并且不会跨会话保持。
  • 通过语言扩展。

变量

Zed 任务的行为就像您的 shell 一样; 这也意味着您可以通过类似 sh 的 $VAR_NAME 语法引用环境变量。 为了方便起见,还设置了几个额外的环境变量。 这些变量允许您从当前编辑器中提取信息并在任务中使用它。 以下变量可用

  • ZED_COLUMN: 当前行所在的列
  • ZED_ROW: 当前行号
  • ZED_FILE: 当前打开文件的绝对路径 (例如, /Users/my-user/path/to/project/src/main.rs)
  • ZED_FILENAME: 当前打开文件的文件名 (例如, main.rs)
  • ZED_DIRNAME: 当前打开文件的绝对路径,不包含文件名 (例如, /Users/my-user/path/to/project/src)
  • ZED_RELATIVE_FILE: 当前打开文件的路径,相对于 ZED_WORKTREE_ROOT (例如, src/main.rs)
  • ZED_STEM: 当前打开文件的主文件名(不带扩展名)(例如, main)
  • ZED_SYMBOL: 当前选定的符号; 应与符号面包屑中显示的最后一个符号匹配 (例如, mod tests > fn test_task_contexts)
  • ZED_SELECTED_TEXT: 当前选定的文本
  • ZED_WORKTREE_ROOT: 当前工作树根目录的绝对路径. (例如, /Users/my-user/path/to/project)
  • ZED_CUSTOM_RUST_PACKAGE: (特定于 Rust) $ZED_FILE 源文件的父包的名称。

要在任务中使用变量,请在变量前加上美元符号 ($)

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

您还可以使用详细的语法,如果给定的变量不可用,则允许指定默认值:${ZED_FILE:default_value}

这些环境变量也可以在任务的 cwdargslabel 字段中使用。

变量引用

当处理包含空格或其他特殊字符的路径时,请确保正确转义变量。

例如,不要这样做(如果路径有空格,则会失败)

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

提供以下内容

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

或者显式包含转义的引号,如下所示

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

临时任务

通过 task: spawn 打开的同一任务模态框支持任意类似 bash 的命令执行:在模态框文本字段中键入命令,然后使用 opt-enter 来生成它。

任务模态框会在会话期间保留这些临时命令,task: rerun 也会重新运行此类任务(如果它们是最后生成的任务)。

您还可以调整模态框中当前选定的任务(tab 是默认的按键绑定)。 这样做会将其命令放入一个提示符中,然后可以将其编辑并生成为临时任务。

临时任务

您可以在通过模态框生成任务时使用 cmd 修饰符; 以这种方式生成的任务不会增加其使用计数(因此,它们不会通过 task: rerun 重新生成,并且它们在任务模态框中不会具有较高的排名)。 临时任务的预期用途是保持持续 task: rerun 使用的流程。

任务的自定义按键绑定

您可以通过向 task::Spawn 传递额外的参数来为您的任务定义自己的按键绑定。 如果您想将上述 echo current file's path 任务绑定到 alt-g,您可以在 keymap.json 文件中添加以下代码段

{
  "context": "Workspace",
  "bindings": {
    "alt-g": ["task::Spawn", { "task_name": "echo current file's path" }]
  }
}

请注意,这些任务还可以指定“target”来控制生成的任务应在何处显示。 这对于启动您想在中心区域使用的终端应用程序很有用

// In tasks.json
{
  "label": "start lazygit",
  "command": "lazygit -p $ZED_WORKTREE_ROOT"
}
// In keymap.json
{
  "context": "Workspace",
  "bindings": {
    "alt-g": [
      "task::Spawn",
      { "task_name": "start lazygit", "reveal_target": "center" }
    ]
  }
}

将可运行标签绑定到任务模板

Zed 支持通过工作区本地和全局 tasks.json 文件覆盖内联可运行指示器的默认操作,优先级如下

  1. 工作区 tasks.json
  2. 全局 tasks.json
  3. 语言提供的标签绑定(默认)。

要标记任务,请将可运行标签名称添加到任务模板上的 tags 字段

{
  "label": "echo current file's path",
  "command": "echo $ZED_FILE",
  "tags": ["rust-test"]
}

这样做,您可以更改在可运行指示器中显示的任务。

运行绑定到可运行对象的任务的按键绑定

当您有一个绑定到可运行对象的任务定义时,您可以使用 代码操作 快速运行它,您可以通过 editor: Toggle Code Actions 命令或 cmd-./ctrl-. 快捷方式来触发代码操作。 您的任务将是下拉列表中的第一个。 如果此行没有其他代码操作,则该任务将立即运行。