1---
2title: Tasks - Run Commands in Zed
3description: Run and rerun shell commands from Zed with task definitions. Supports variables, templates, and language-specific tasks.
4---
5
6# Tasks
7
8Zed supports ways to spawn (and rerun) commands using its integrated [terminal](./terminal.md) to output the results. These commands can read a limited subset of Zed state (such as a path to the file currently being edited or selected text).
9
10```json [tasks]
11[
12 {
13 "label": "Example task",
14 "command": "for i in {1..5}; do echo \"Hello $i/5\"; sleep 1; done",
15 //"args": [],
16 // Env overrides for the command, will be appended to the terminal's environment from the settings.
17 "env": { "foo": "bar" },
18 // Current working directory to spawn the command into, defaults to current project root.
19 //"cwd": "/path/to/working/directory",
20 // Whether to use a new terminal tab or reuse the existing one to spawn the process, defaults to `false`.
21 "use_new_terminal": false,
22 // Whether to allow multiple instances of the same task to be run, or rather wait for the existing ones to finish, defaults to `false`.
23 "allow_concurrent_runs": false,
24 // What to do with the terminal pane and tab, after the command was started:
25 // * `always` — always show the task's pane, and focus the corresponding tab in it (default)
26 // * `no_focus` — always show the task's pane, add the task's tab in it, but don't focus it
27 // * `never` — do not alter focus, but still add/reuse the task's tab in its pane
28 "reveal": "always",
29 // What to do with the terminal pane and tab, after the command has finished:
30 // * `never` — Do nothing when the command finishes (default)
31 // * `always` — always hide the terminal tab, hide the pane also if it was the last tab in it
32 // * `on_success` — hide the terminal tab on task success only, otherwise behaves similar to `always`
33 "hide": "never",
34 // Which shell to use when running a task inside the terminal.
35 // May take 3 values:
36 // 1. (default) Use the system's default terminal configuration in /etc/passwd
37 // "shell": "system"
38 // 2. A program:
39 // "shell": {
40 // "program": "sh"
41 // }
42 // 3. A program with arguments:
43 // "shell": {
44 // "with_arguments": {
45 // "program": "/bin/bash",
46 // "args": ["--login"]
47 // }
48 // }
49 "shell": "system",
50 // Whether to show the task line in the output of the spawned task, defaults to `true`.
51 "show_summary": true,
52 // Whether to show the command line in the output of the spawned task, defaults to `true`.
53 "show_command": true
54 // Represents the tags for inline runnable indicators, or spawning multiple tasks at once.
55 // "tags": []
56 }
57]
58```
59
60There are two actions that drive the workflow of using tasks: `task: spawn` and `task: rerun`.
61`task: spawn` opens a modal with all available tasks in the current file.
62`task: rerun` reruns the most recently spawned task. You can also rerun tasks from the task modal.
63
64By default, rerunning tasks reuses the same terminal (due to the `"use_new_terminal": false` default) but waits for the previous task to finish before starting (due to the `"allow_concurrent_runs": false` default).
65
66Keep `"use_new_terminal": false` and set `"allow_concurrent_runs": true` to allow cancelling previous tasks on rerun.
67
68## Task templates
69
70Tasks can be defined:
71
72- in the global `tasks.json` file; such tasks are available in all Zed projects you work on. This file is usually located in `~/.config/zed/tasks.json`. You can edit them by using the `zed: open tasks` action.
73- in the worktree-specific (local) `.zed/tasks.json` file; such tasks are available only when working on a project with that worktree included. You can edit worktree-specific tasks by using the `zed: open project tasks` action.
74- on the fly with [oneshot tasks](#oneshot-tasks). These tasks are project-specific and do not persist across sessions.
75- by language extension.
76
77## Variables
78
79Zed tasks act just like your shell; that also means that you can reference environmental variables via sh-esque `$VAR_NAME` syntax. A couple of additional environmental variables are set for your convenience.
80These variables allow you to pull information from the current editor and use it in your tasks. The following variables are available:
81
82- `ZED_COLUMN`: current line column
83- `ZED_ROW`: current line row
84- `ZED_FILE`: absolute path of the currently opened file (e.g. `/Users/my-user/path/to/project/src/main.rs`)
85- `ZED_FILENAME`: filename of the currently opened file (e.g. `main.rs`)
86- `ZED_DIRNAME`: absolute path of the currently opened file with file name stripped (e.g. `/Users/my-user/path/to/project/src`)
87- `ZED_RELATIVE_FILE`: path of the currently opened file, relative to `ZED_WORKTREE_ROOT` (e.g. `src/main.rs`)
88- `ZED_RELATIVE_DIR`: path of the currently opened file's directory, relative to `ZED_WORKTREE_ROOT` (e.g. `src`)
89- `ZED_STEM`: stem (filename without extension) of the currently opened file (e.g. `main`)
90- `ZED_SYMBOL`: currently selected symbol; should match the last symbol shown in a symbol breadcrumb (e.g. `mod tests > fn test_task_contexts`)
91- `ZED_SELECTED_TEXT`: currently selected text
92- `ZED_WORKTREE_ROOT`: absolute path to the root of the current worktree. (e.g. `/Users/my-user/path/to/project`)
93- `ZED_CUSTOM_RUST_PACKAGE`: (Rust-specific) name of the parent package of $ZED_FILE source file.
94
95To use a variable in a task, prefix it with a dollar sign (`$`):
96
97```json [tasks]
98{
99 "label": "echo current file's path",
100 "command": "echo $ZED_FILE"
101}
102```
103
104You can also use verbose syntax that allows specifying a default if a given variable is not available: `${ZED_FILE:default_value}`
105
106These environmental variables can also be used in tasks' `cwd`, `args`, and `label` fields.
107
108### Variable Quoting
109
110When working with paths containing spaces or other special characters, please ensure variables are properly escaped.
111
112For example, instead of this (which will fail if the path has a space):
113
114```json [tasks]
115{
116 "label": "stat current file",
117 "command": "stat $ZED_FILE"
118}
119```
120
121Provide the following:
122
123```json [tasks]
124{
125 "label": "stat current file",
126 "command": "stat",
127 "args": ["$ZED_FILE"]
128}
129```
130
131Or explicitly include escaped quotes like so:
132
133```json [tasks]
134{
135 "label": "stat current file",
136 "command": "stat \"$ZED_FILE\""
137}
138```
139
140### Task filtering based on variables
141
142Task definitions with variables which are not present at the moment the task list is determined are filtered out.
143For example, the following task will appear in the spawn modal only if there is a text selection:
144
145```json [tasks]
146{
147 "label": "selected text",
148 "command": "echo \"$ZED_SELECTED_TEXT\""
149}
150```
151
152Set default values to such variables to have such tasks always displayed:
153
154```json [tasks]
155{
156 "label": "selected text with default",
157 "command": "echo \"${ZED_SELECTED_TEXT:no text selected}\""
158}
159```
160
161## Oneshot tasks
162
163The 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.
164
165The 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.
166
167You 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.
168
169### Ephemeral tasks
170
171You 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).
172The intended use of ephemeral tasks is to stay in the flow with continuous `task: rerun` usage.
173
174### More task rerun control
175
176By default, tasks capture their variables into a context once, and this "resolved task" is being rerun always.
177
178This can be controlled with the `"reevaluate_context"` argument to the task: setting it to `true` will force the task to be reevaluated before each run.
179
180```json [keymap]
181{
182 "context": "Workspace",
183 "bindings": {
184 "alt-t": ["task::Rerun", { "reevaluate_context": true }]
185 }
186}
187```
188
189## Custom keybindings for tasks
190
191You can define your own keybindings for your tasks via an additional argument to `task::Spawn`. If you wanted to bind the aforementioned `echo current file's path` task to `alt-g`, you would add the following snippet in your [`keymap.json`](./key-bindings.md) file:
192
193```json [keymap]
194{
195 "context": "Workspace",
196 "bindings": {
197 "alt-g": ["task::Spawn", { "task_name": "echo current file's path" }]
198 }
199}
200```
201
202Note that these tasks can also have a 'target' specified to control where the spawned task should show up.
203This could be useful for launching a terminal application that you want to use in the center area:
204
205```json [tasks]
206// In tasks.json
207{
208 "label": "start lazygit",
209 "command": "lazygit -p $ZED_WORKTREE_ROOT"
210}
211```
212
213```json [keymap]
214// In keymap.json
215{
216 "context": "Workspace",
217 "bindings": {
218 "alt-g": [
219 "task::Spawn",
220 { "task_name": "start lazygit", "reveal_target": "center" }
221 ]
222 }
223}
224```
225
226## VS Code Task Format
227
228When importing VS Code tasks from `.vscode/tasks.json`, you can omit the `label` field. Zed automatically generates labels based on the task type:
229
230- **npm tasks**: `npm: <script>` (e.g., `npm: start`)
231- **gulp tasks**: `gulp: <task>` (e.g., `gulp: build`)
232- **shell tasks**: Uses the `command` string directly (e.g., `echo hello`), or `shell` if the command is empty
233- **Tasks without type**: `Untitled Task`
234
235Example task file with auto-generated labels:
236
237```json
238{
239 "version": "2.0.0",
240 "tasks": [
241 {
242 "type": "npm",
243 "script": "start"
244 },
245 {
246 "type": "shell",
247 "command": "cargo build --release"
248 }
249 ]
250}
251```
252
253These tasks appear in the task picker as "npm: start" and "cargo build --release". You can override the generated label by providing an explicit `label` field.
254
255## Binding runnable tags to task templates
256
257Zed supports overriding the default action for inline runnable indicators via workspace-local and global `tasks.json` file with the following precedence hierarchy:
258
2591. Workspace `tasks.json`
2602. Global `tasks.json`
2613. Language-provided tag bindings (default).
262
263To tag a task, add the runnable tag name to the `tags` field on the task template:
264
265```json [tasks]
266{
267 "label": "echo current file's path",
268 "command": "echo $ZED_FILE",
269 "tags": ["rust-test"]
270}
271```
272
273In doing so, you can change which task is shown in the runnables indicator.
274
275## Keybindings to run tasks bound to runnables
276
277When you have a task definition that is bound to the runnable, you can quickly run it using [Code Actions](https://zed.dev/docs/configuring-languages?#code-actions) that you can trigger either via `editor: Toggle Code Actions` command or by the `cmd-.`/`ctrl-.` shortcut. Your task will be the first in the dropdown. The task will run immediately if there are no additional Code Actions for this line.
278
279## Running Bash Scripts
280
281You can run bash scripts directly from Zed. When you open a `.sh` or `.bash` file, Zed automatically detects the script as runnable and makes it available in the task picker.
282
283To run a bash script:
284
2851. Open the command palette with {#kb command_palette::Toggle}
2862. Search for "task" and select **task: spawn**
2873. Select the script from the list
288
289Bash scripts are tagged with `bash-script`, allowing you to filter or reference them in task configurations.
290
291If you need to pass arguments or customize the execution environment, add a task configuration in your `.zed/tasks.json`:
292
293```json
294[
295 {
296 "label": "run my-script.sh with args",
297 "command": "./my-script.sh",
298 "args": ["--verbose", "--output=results.txt"],
299 "tags": ["bash-script"]
300 }
301]
302```
303
304## Shell Initialization
305
306When Zed runs a task, it launches the command in a login shell. This ensures your shell's initialization files (`.bash_profile`, `.zshrc`, etc.) are sourced before the task executes.
307
308This behavior gives tasks access to the same environment variables, aliases, and PATH modifications you've configured in your shell profile. If a task fails to find a command that works in your terminal, verify your shell configuration files are properly set up.
309
310To override the shell used for tasks, configure the `terminal.shell` setting:
311
312```json
313{
314 "terminal": {
315 "shell": {
316 "program": "/bin/zsh"
317 }
318 }
319}
320```
321
322See [Terminal configuration](./terminal.md) for complete shell options.