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_LANGUAGE`: language of the currently opened buffer (e.g. `Rust`, `Python`, `Shell Script`)
93- `ZED_WORKTREE_ROOT`: absolute path to the root of the current worktree. (e.g. `/Users/my-user/path/to/project`)
94- `ZED_CUSTOM_RUST_PACKAGE`: (Rust-specific) name of the parent package of $ZED_FILE source file.
95
96To use a variable in a task, prefix it with a dollar sign (`$`):
97
98```json [tasks]
99{
100 "label": "echo current file's path",
101 "command": "echo $ZED_FILE"
102}
103```
104
105You can also use verbose syntax that allows specifying a default if a given variable is not available: `${ZED_FILE:default_value}`
106
107These environmental variables can also be used in tasks' `cwd`, `args`, and `label` fields.
108
109### Variable Quoting
110
111When working with paths containing spaces or other special characters, please ensure variables are properly escaped.
112
113For example, instead of this (which will fail if the path has a space):
114
115```json [tasks]
116{
117 "label": "stat current file",
118 "command": "stat $ZED_FILE"
119}
120```
121
122Provide the following:
123
124```json [tasks]
125{
126 "label": "stat current file",
127 "command": "stat",
128 "args": ["$ZED_FILE"]
129}
130```
131
132Or explicitly include escaped quotes like so:
133
134```json [tasks]
135{
136 "label": "stat current file",
137 "command": "stat \"$ZED_FILE\""
138}
139```
140
141### Task filtering based on variables
142
143Task definitions with variables which are not present at the moment the task list is determined are filtered out.
144For example, the following task will appear in the spawn modal only if there is a text selection:
145
146```json [tasks]
147{
148 "label": "selected text",
149 "command": "echo \"$ZED_SELECTED_TEXT\""
150}
151```
152
153Set default values to such variables to have such tasks always displayed:
154
155```json [tasks]
156{
157 "label": "selected text with default",
158 "command": "echo \"${ZED_SELECTED_TEXT:no text selected}\""
159}
160```
161
162## Oneshot tasks
163
164The 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.
165
166The 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.
167
168You 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.
169
170### Ephemeral tasks
171
172You 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).
173The intended use of ephemeral tasks is to stay in the flow with continuous `task: rerun` usage.
174
175### More task rerun control
176
177By default, tasks capture their variables into a context once, and this "resolved task" is being rerun always.
178
179This 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.
180
181```json [keymap]
182{
183 "context": "Workspace",
184 "bindings": {
185 "alt-t": ["task::Rerun", { "reevaluate_context": true }]
186 }
187}
188```
189
190## Custom keybindings for tasks
191
192You 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:
193
194```json [keymap]
195{
196 "context": "Workspace",
197 "bindings": {
198 "alt-g": ["task::Spawn", { "task_name": "echo current file's path" }]
199 }
200}
201```
202
203Note that these tasks can also have a 'target' specified to control where the spawned task should show up.
204This could be useful for launching a terminal application that you want to use in the center area:
205
206```json [tasks]
207// In tasks.json
208{
209 "label": "start lazygit",
210 "command": "lazygit -p $ZED_WORKTREE_ROOT"
211}
212```
213
214```json [keymap]
215// In keymap.json
216{
217 "context": "Workspace",
218 "bindings": {
219 "alt-g": [
220 "task::Spawn",
221 { "task_name": "start lazygit", "reveal_target": "center" }
222 ]
223 }
224}
225```
226
227## VS Code Task Format
228
229When importing VS Code tasks from `.vscode/tasks.json`, you can omit the `label` field. Zed automatically generates labels based on the task type:
230
231- **npm tasks**: `npm: <script>` (e.g., `npm: start`)
232- **gulp tasks**: `gulp: <task>` (e.g., `gulp: build`)
233- **shell tasks**: Uses the `command` string directly (e.g., `echo hello`), or `shell` if the command is empty
234- **Tasks without type**: `Untitled Task`
235
236Example task file with auto-generated labels:
237
238```json
239{
240 "version": "2.0.0",
241 "tasks": [
242 {
243 "type": "npm",
244 "script": "start"
245 },
246 {
247 "type": "shell",
248 "command": "cargo build --release"
249 }
250 ]
251}
252```
253
254These 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.
255
256## Binding runnable tags to task templates
257
258Zed supports overriding the default action for inline runnable indicators via workspace-local and global `tasks.json` file with the following precedence hierarchy:
259
2601. Workspace `tasks.json`
2612. Global `tasks.json`
2623. Language-provided tag bindings (default).
263
264To tag a task, add the runnable tag name to the `tags` field on the task template:
265
266```json [tasks]
267{
268 "label": "echo current file's path",
269 "command": "echo $ZED_FILE",
270 "tags": ["rust-test"]
271}
272```
273
274In doing so, you can change which task is shown in the runnables indicator.
275
276## Keybindings to run tasks bound to runnables
277
278When 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.
279
280## Running Bash Scripts
281
282You 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.
283
284To run a bash script:
285
2861. Open the command palette with {#kb command_palette::Toggle}
2872. Search for "task" and select **task: spawn**
2883. Select the script from the list
289
290Bash scripts are tagged with `bash-script`, allowing you to filter or reference them in task configurations.
291
292If you need to pass arguments or customize the execution environment, add a task configuration in your `.zed/tasks.json`:
293
294```json
295[
296 {
297 "label": "run my-script.sh with args",
298 "command": "./my-script.sh",
299 "args": ["--verbose", "--output=results.txt"],
300 "tags": ["bash-script"]
301 }
302]
303```
304
305## Shell Initialization
306
307When 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.
308
309This 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.
310
311To override the shell used for tasks, configure the `terminal.shell` setting:
312
313```json
314{
315 "terminal": {
316 "shell": {
317 "program": "/bin/zsh"
318 }
319 }
320}
321```
322
323See [Terminal configuration](./terminal.md) for complete shell options.