function zssh --description "SSH to a host and attach to a remote zmx session" argparse 'h/help' 'install' -- $argv or return 1 if set -q _flag_help; or test (count $argv) -lt 1 echo "Usage: zssh [--install] [user@]host [session]" echo "Example: zssh devbox term" return 0 end set -l destination $argv[1] set -l session main if test (count $argv) -ge 2 set session $argv[2] end if not string match -qr '^[A-Za-z0-9._:-]+$' -- $session echo "zssh: session names may only contain letters, numbers, dots, underscores, colons, and hyphens" >&2 return 2 end mkdir -p "$HOME/.ssh/controlmasters" if set -q _flag_install zmx-remote-install $destination or return $status else zmx-remote-install --upgrade-only $destination or return $status end set -l remote_command "sh -c 'PATH=\"\$HOME/.local/bin:\$PATH\"; command -v zmx >/dev/null 2>&1 || { echo \"zssh: zmx is not installed on the remote host or is not on PATH; retry with zssh --install\" >&2; exit 127; }; exec zmx attach \"\$1\"' sh '$session'" command ssh \ -o ControlMaster=auto \ -o ControlPersist=10m \ -o ControlPath="$HOME/.ssh/controlmasters/%C" \ -o RequestTTY=yes \ -o RemoteCommand="$remote_command" \ $destination end