Merging all Tmux windows in a single session
I’m using a new Tmux session for each day. In time, I noticed sessions proliferate and I may need some of the windows from earlier sessions in a central place.
This script merges all windows in all Tmux sessions in a single session. You must specify the session to collect all as a parameter. If you save the script as merge-tmux-sessions.zsh
, you can run it like:
merge-tmux-sessions.zsh my-current-session
and it will move all windows under the session you specify.
#!/bin/zsh
# set -vuex
#
# Specify the target session
target_session=$1
# Get the list of all sessions
sessions=$(tmux list-sessions -F '#S')
# Iterate through each session
for session in ${(@f)sessions}; do
if [ "$session" != "$target_session" ]; then
# Get the list of windows in the current session
windows=$(tmux list-windows -t $session -F '#I')
# Move each window to the target session
for window in ${(@f)windows}; do
tmux move-window -s ${session}:${window} -t ${target_session}
done
fi
done