Skip to content

Instantly share code, notes, and snippets.

@klaxalk
Created July 3, 2017 12:30
Show Gist options
  • Save klaxalk/a14f52695fab89e89898d6ae3768f1d2 to your computer and use it in GitHub Desktop.
Save klaxalk/a14f52695fab89e89898d6ae3768f1d2 to your computer and use it in GitHub Desktop.
Example of tmux script
#!/bin/bash
SESSION_NAME=my_session
# following commands will be executed first, in each window
pre_input="echo this will run in every window before as a first command"
# define commands
# 'name' 'command'
input=(
'Window1' "vim
"
'Some other window' "ls -lah"
)
if [ -z ${TMUX} ];
then
TMUX= tmux new-session -s $SESSION_NAME -d
echo "Starting new session."
else
SESSION_NAME="$(tmux display-message -p '#S')"
fi
# create arrays of names and commands
for ((i=0; i < ${#input[*]}; i++));
do
((i%2==0)) && names[$i/2]="${input[$i]}"
((i%2==1)) && cmds[$i/2]="${input[$i]}"
done
# run tmux windows
for ((i=0; i < ${#names[*]}; i++));
do
tmux new-window -t $SESSION_NAME:$(($i+10)) -n "${names[$i]}"
done
sleep 4
# send commands
for ((i=0; i < ${#cmds[*]}; i++));
do
tmux send-keys -t $SESSION_NAME:$(($i+10)) "${pre_input};
${cmds[$i]}"
done
sleep 4
tmux select-window -t $SESSION_NAME:0
tmux -2 attach-session -t $SESSION_NAME
clear
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment