Skip to content

Instantly share code, notes, and snippets.

@yszheda
Last active November 17, 2016 06:54
Show Gist options
  • Save yszheda/9138288 to your computer and use it in GitHub Desktop.
Save yszheda/9138288 to your computer and use it in GitHub Desktop.
take and restore tmux snapshot
#!/bin/bash
# take and restore tmux snapshot
tmuxSnapshot="/.tmux_snapshot"
tmuxEXE=`which tmux`
save_snap()
{
${tmuxEXE} list-windows -a -F"#{session_name} #{window_name} #{pane_current_command} #{pane_current_path}" > ${tmuxSnapshot}
}
restore_snap()
{
${tmuxEXE} start-server
while IFS=' ' read -r session_name window_name pane_current_command pane_current_path
do
${tmuxEXE} has-session -t "${session_name}" 2>/dev/null
if [ $? != 0 ]
then
${tmuxEXE} new-session -d -s "${session_name}" -n ${window_name}
else
${tmuxEXE} new-window -d -t ${session_name} -n "${window_name}"
fi
${tmuxEXE} send-keys -t "${session_name}:${window_name}" "cd ${pane_current_path}; echo \"Hint: last time you are executing '${pane_current_command}'.\"" ENTER
done < ${tmuxSnapshot}
}
ps aux|grep -w tmux|grep -v grep
if [ $? != 0 ]
then
restore_snap
else
save_snap
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment