Skip to content

Instantly share code, notes, and snippets.

@zjx20
Last active June 6, 2022 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zjx20/0fd10e3d509aac9412015cdba47abf8c to your computer and use it in GitHub Desktop.
Save zjx20/0fd10e3d509aac9412015cdba47abf8c to your computer and use it in GitHub Desktop.
broadcast input to multiple hosts simultaneously with tmux & zsh. idea from https://www.reddit.com/r/tmux/comments/lqp9sd/send_keystrokes_to_multiple_panes_like_iterm2/
# usage: multi ssh the-host{0,1,2}
function multi {
cmd=$1
shift
if [ -z "$TMUX" ]; then
session="multi-${cmd}-$(head -c 4 /dev/urandom | base64 | tr '/+' '_-' | tr -d '=')"
tmux new -d -s "${session}"
else
session=$(tmux display-message -p '#S')
fi
window="${session}$(tmux display-message -p ':#I')"
tmux send-keys -t ${window}.0 "$cmd ${@[1]}"
for ((pane = 2; pane <= ${#@[@]}; pane++)); do
tmux splitw -h -t $window
tmux send-keys -t ${window}.$((pane-1)) "$cmd ${@[pane]}"
tmux select-layout -t $window tiled > /dev/null
done
tmux set-window-option -t $window synchronize-panes on > /dev/null
tmux set-window-option -t $window pane-active-border-style fg=red > /dev/null
tmux set-window-option -t $window pane-border-style fg=yellow > /dev/null
tmux send-keys -t ${window}.0 Enter
tmux new -A -s ${session}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment