If you're reading this article, you're likely already familiar with the Zed Editor — a Rust-based, open-source, multiplayer code editor .As an open-source enthusiast, I always try to use and support open-source tools, software or anything, and Zed stands out as a truly next-generation code editor. Designed for speed and collaboration, it delivers an ultra-fast, intelligent, and responsive coding experience by leveraging multiple CPU cores and GPU acceleration.
When I first tried to execute my C++ program in Zed, I encountered a few challenges. Unlike traditional IDEs, Zed is largely script-driven, meaning you need to configure custom tasks via settings to execute programs. Since there’s limited beginner-friendly documentation on this, I wanted to share my experience to help others navigate this process. In this article, I’ll Walk through how to run a C++ program in the Zed Editor, step by step. I am using Fedora 42 as an operating system while testing that script.
Setup C++ Compiler
First, ensure you have a C++ compiler installed, such as g++ or clang++ in your system
Configure Clangd in Zed (Optional for language support):
Zed uses clangd
as the language server for C++ support and formatting. You can configure the path to your clangd compiler in settings.json:
use this type script.
For accessing settings.jso
, open settings from Zed.
"lsp": {
"clangd": {
"binary": {
"path": "/path/to/clangd",
"arguments": []
}
}
}
Create a Task to Compile and Run:
Use Zed's "tasks" feature to automate compiling and running your C++ code. For this follow the steps,
Open the Tasks Configuration:
1.In Zed, open the Command Palette by pressing Ctrl+Shift+P (Linux) or Cmd+Shift+P (Mac).
2.Type zed: open tasks
and select it to open the tasks configuration file (tasks.json
). This can be a global file (~/.config/zed/tasks.json
) for all projects or a local file (.zed/tasks.json
) for the current project.
Add a Task Entry:
Inside the JSON array, add a new object representing your task. For example, to compile and run a C++ program:
[
{
"label": "Compile, Run, and Extra Commands",
"command": "g++ $ZED_FILE -o $ZED_STEM && ./$ZED_STEM && echo && echo 'Program finished' && echo",
"use_new_terminal": false,
"allow_concurrent_runs": false,
"reveal": "always",
"hide": "never",
"shell": "system"
}
]
Here,
$ZED_FILE
is the current C++ source file.$ZED_STEM is that filename without extension, used as the executable name.
The commands run sequentially:
1.Compile the C++ file with g++.
2. Run the produced executable.
3. Print a message (echo 'Program finished').
4. List files in the directory (ls).
If want to extra command to add
Define a new task with a command string that includes multiple shell commands separated by && or ; depending on whether you want them to stop on failure or run regardless
Save the File:
The tasks.json file will save your task.
Run the Task:
- Open the Command Palette again.
- Type task: spawn and select your "Compile and Run C++" task.
- The output of the compilation and running will appear in the integrated terminal.
Now,You can run C++ program in Zed text editor.
How to set Keymap for execute the program:
If you want to set key bindings for execute the program, then maybe these steps helpful for you.
Open your Zed keymap configuration:Open the command palette with
Ctrl+Shift+P (or Cmd+Shift+P)
and typeZed: Open Keymap
.Add a binding for your custom shortcut:
then edit keymap.json with this template. Here I write for the snippet the first one
[
{
"context": "Workspace",
"bindings": {
"shift-e": ["task::Spawn", { "task_name": "Compile, Run, and Extra Commands" }]
}
}
]
The task_name string must exactly match the "label" in your tasks.json (including capitalization, spaces, and punctuation).
"shift-e" is my desired shortcut.
Save the file and restart Zed.
Now, pressing shift+e will run your entire C++ program with a single keystroke, just like VS Code's "Run" button, without needing to use the Command Palette
How to create snippet
Follow these steps, to create any snippet for c++ program.
Open Zed’s command palette (
Ctrl+Shift+P or Cmd+Shift+P
).Select snippets: configure snippets.
Choose C++ (c++.json) or create if it doesn't exist
Paste the above JSON snippet inside the file (make sure it’s valid JSON with commas if you add multiple snippets).
Save the file.
In C++ files, type cppmain and press tab to expand.
Let, I am here writing a simple c++ snippet for main function:
{
"C++ main template": {
"prefix": "cppmain",
"body": [
"#include <bits/stdc++.h>",
"using namespace std;",
"",
"int main()",
"{",
" $0 ",
" ",
" ",
" cout << endl;",
" return 0;",
"}"
],
"description": "C++ main function template with bits/stdc++.h"
},
"Another snippet": {
"prefix": "as",
"body": ["// Additional snippet here"],
"description": "Example snippet"
}
}
So, what we do here:
- prefix: "cppmain" — This is the trigger text for the snippet.
- body: An array of strings each representing a line in the snippet.
- $0 is correctly placed to mark the final cursor position inside the main function.
Just make sure:
This snippet object is inside the main JSON snippet file for C++ (usually cpp.json).
If you have multiple snippets in the same file, remember to separate them by commas.
Top comments (0)