Skip to content

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

  1. Select text in the editor and press Ctrl+Alt+Enter
  2. Zed runs the RunSelected task, which calls ~/.local/bin/zed-send
  3. zed-send writes the selected text to /tmp/.zed-pending-cmd
  4. zed-send finds the Zed terminal's PTY and writes a DSR escape sequence (ESC[5n) to it
  5. Zed's terminal emulator responds with ESC[0n into bash's input stream
  6. bash readline triggers the __zed_run function (bound via bind -x)
  7. __zed_run reads the command file and evals 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 ~/.bashrc

2. Remove the zed-send script

bash
rm ~/.local/bin/zed-send

3. 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-cmd

Requirements

  • Zed editor
  • bash (uses bind -x and READLINE_LINE)
  • Linux (uses /proc filesystem to find terminal PTY)

Limitations

  • Only works with bash (not zsh/fish) due to bind -x and READLINE_LINE
  • Only targets one Zed terminal - if multiple are open, it picks the first one found
  • The command runs via eval so shell aliases and functions work, but complex multi-line selections may behave unexpectedly
  • Requires source ~/.bashrc or a new terminal after first setup

Released under the MIT License.