Created
October 19, 2026 18:08
-
-
Save orip/fd6361d78dce87fae7316a5ace4ea13a to your computer and use it in GitHub Desktop.
Kill current wezterm workspace with confirmation (Python script)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| """Kill all panes in the current wezterm workspace.""" | |
| import argparse | |
| import json | |
| import os | |
| import shutil | |
| import subprocess | |
| import sys | |
| def die(msg): | |
| print(f"error: {msg}", file=sys.stderr) | |
| sys.exit(1) | |
| def wezterm_cli(*args): | |
| try: | |
| return subprocess.run( | |
| ["wezterm", "cli", *args], check=True, capture_output=True, text=True | |
| ).stdout | |
| except subprocess.CalledProcessError as e: | |
| die(f"'wezterm cli {' '.join(args)}' failed: {e.stderr.strip() or e}") | |
| def main(): | |
| parser = argparse.ArgumentParser( | |
| description="Kill every pane in the current wezterm workspace" | |
| ) | |
| parser.add_argument("-n", "--dry-run", action="store_true", | |
| help="print what would be killed, kill nothing") | |
| parser.add_argument("-y", "--yes", action="store_true", | |
| help="skip the confirmation prompt") | |
| args = parser.parse_args() | |
| if shutil.which("wezterm") is None: | |
| die("wezterm not found in PATH") | |
| cur_env = os.environ.get("WEZTERM_PANE") | |
| if not cur_env: | |
| die("WEZTERM_PANE not set; not running inside wezterm?") | |
| try: | |
| cur_pane_id = int(cur_env) | |
| except ValueError: | |
| die(f"WEZTERM_PANE is not a pane id: {cur_env!r}") | |
| try: | |
| all_panes = json.loads(wezterm_cli("list", "--format", "json") or "[]") | |
| except json.JSONDecodeError as e: | |
| die(f"can't parse 'wezterm cli list' output: {e}") | |
| if not all_panes: | |
| die("wezterm reported no panes") | |
| cur_pane = next((p for p in all_panes if p["pane_id"] == cur_pane_id), None) | |
| if cur_pane is None: | |
| die(f"current pane {cur_pane_id} not found in 'wezterm cli list'") | |
| workspace = cur_pane["workspace"] | |
| # Other panes first, current pane last: killing our own pane kills this script. | |
| panes = [p for p in all_panes if p["workspace"] == workspace] | |
| panes.sort(key=lambda p: p["pane_id"] == cur_pane_id) | |
| num_tabs = len({p["tab_id"] for p in panes}) | |
| print(f"Workspace '{workspace}': {num_tabs} tabs, {len(panes)} panes") | |
| for p in panes: | |
| print(f" tab {p['tab_id']} pane {p['pane_id']} {p.get('tab_title') or p.get('title', '')}") | |
| if args.dry_run: | |
| for p in panes: | |
| print(f"[dry-run] wezterm cli kill-pane --pane-id {p['pane_id']}") | |
| return | |
| if not args.yes: | |
| try: | |
| answer = input(f"Kill current workspace '{workspace}' with {num_tabs} tabs? [y/N] ") | |
| except (EOFError, KeyboardInterrupt): | |
| print() | |
| answer = "" | |
| if answer.strip().lower() not in ("y", "yes"): | |
| print("aborted") | |
| sys.exit(1) | |
| for p in panes: | |
| wezterm_cli("kill-pane", "--pane-id", str(p["pane_id"])) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment