Zed: Send Selection to Terminal
Run selected text from the Zed editor directly in your existing Zed terminal using Ctrl+Alt+Enter.
How It Works
- Select text in the editor and press
Ctrl+Alt+Enter - Zed runs the
RunSelectedtask, which calls~/.local/bin/zed-send zed-sendwrites the selected text to/tmp/.zed-pending-cmdzed-sendfinds the Zed terminal's PTY and writes a DSR escape sequence (ESC[5n) to it- Zed's terminal emulator responds with
ESC[0ninto bash's input stream - bash readline triggers the
__zed_runfunction (bound viabind -x) __zed_runreads the command file andevals it in the terminal's current session
No background processes, no listeners, no manual setup required.
Files Modified
~/.config/zed/keymap.json
Added keybinding in the Editor context:
json
{
"context": "Editor",
"bindings": {
"ctrl-alt-enter": ["task::Spawn", { "task_name": "RunSelected" }]
}
}~/.config/zed/tasks.json
Added the RunSelected task:
json
{
"label": "RunSelected",
"command": "zed-send",
"use_new_terminal": false,
"allow_concurrent_runs": false,
"reveal": "never",
"hide": "always",
"show_summary": false,
"show_command": false
}~/.local/bin/zed-send
Script that writes the selected text to a temp file and triggers the terminal via DSR escape sequence.
bash
#!/bin/bash
CMD_FILE="/tmp/.zed-pending-cmd"
if [ -z "$ZED_SELECTED_TEXT" ]; then
exit 1
fi
printf '%s\n' "$ZED_SELECTED_TEXT" > "$CMD_FILE"
for pid in $(pgrep -u "$USER" bash); do
[ -r "/proc/$pid/environ" ] || continue
if grep -qz "TERM_PROGRAM=zed" "/proc/$pid/environ" 2>/dev/null; then
if [ "$pid" != "$$" ] && [ "$pid" != "$PPID" ]; then
PTS=$(readlink /proc/$pid/fd/0 2>/dev/null)
if [ -n "$PTS" ] && [ -c "$PTS" ]; then
printf '\033[5n' > "$PTS"
exit 0
fi
fi
fi
done
exit 1~/.bashrc
Added at the end of the file:
bash
# Zed "Send to Terminal" - run commands sent from Zed editor
# Uses DSR escape sequence trick to inject into readline without user input.
__zed_run() {
local cmd_file="/tmp/.zed-pending-cmd"
if [ -f "$cmd_file" ]; then
local cmd
cmd=$(<"$cmd_file")
rm -f "$cmd_file"
printf '\033[1;34m$\033[0m %s\n' "$cmd"
eval "$cmd"
READLINE_LINE=""
READLINE_POINT=0
fi
}
bind -x '"\e[0n": __zed_run'Undo Steps
If something goes wrong, follow these steps to fully revert.
1. Remove the bashrc hook
Edit ~/.bashrc and delete the block at the bottom starting from the comment # Zed "Send to Terminal" through the bind -x line (the last ~12 lines). Or run:
bash
head -n -12 ~/.bashrc > /tmp/.bashrc.clean && mv /tmp/.bashrc.clean ~/.bashrc
source ~/.bashrc2. Remove the zed-send script
bash
rm ~/.local/bin/zed-send3. Remove the Zed task
Edit ~/.config/zed/tasks.json and delete the RunSelected task block.
4. Remove the Zed keybinding
Edit ~/.config/zed/keymap.json and delete the ctrl-alt-enter binding block.
5. Clean up temp files
bash
rm -f /tmp/.zed-pending-cmdRequirements
- Zed editor
- bash (uses
bind -xandREADLINE_LINE) - Linux (uses
/procfilesystem to find terminal PTY)
Limitations
- Only works with bash (not zsh/fish) due to
bind -xandREADLINE_LINE - Only targets one Zed terminal - if multiple are open, it picks the first one found
- The command runs via
evalso shell aliases and functions work, but complex multi-line selections may behave unexpectedly - Requires
source ~/.bashrcor a new terminal after first setup