Skip to content

Instantly share code, notes, and snippets.

@ericvanjohnson
Created October 18, 2023 18:44
Show Gist options
  • Save ericvanjohnson/6a75d505eccc7b04b1cd780f09854fe7 to your computer and use it in GitHub Desktop.
Save ericvanjohnson/6a75d505eccc7b04b1cd780f09854fe7 to your computer and use it in GitHub Desktop.
Launch Tmux Project sessions
#!/usr/bin/env bash
# Initialize variables for widescreens
multiPane=false
# Process options
while getopts "w" opt; do
case $opt in
w)
multiPane=true
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
shift $((OPTIND - 1))
# Credit to ThePrimeagen
if [[ $# -eq 1 ]]; then
selected=$1
else
items=$(find /media/eric/gotham/Repos -maxdepth 2 -mindepth 1 -type d)
items+=$(echo -e "\n/tmp")
selected=$(echo "$items" | fzf)
fi
dirname=$(basename $selected | sed 's/\./_/g')
tmux switch-client -t =$dirname
if [[ $? -eq 0 ]]; then
exit 0
fi
# Create a new tmux session
tmux new-session -c $selected -d -s $dirname && tmux switch-client -t $dirname || tmux new -c $selected -A -s $dirname
# If multiPane is passed, layout the new session
if [[ $multiPane == "true" ]]; then
sleep 2
# Use send-keys to send the tmux pane creation commands to the new session
tmux send-key -t $dirname:1.1 "tmux split-window -h -p 80 -c \"#{pane_current_path}\"" C-m
tmux send-key -t $dirname:1.1 "tmux split-window -h -p 20 -c \"#{pane_current_path}\"" C-m
tmux send-key -t $dirname:1.1 "tmux select-pane -t 1" C-m
tmux send-key -t $dirname:1.1 "tmux split-window -v -p 30 -c \"#{pane_current_path}\"" C-m
sleep 2 # Allow some time for pane creation
# Clear all panes
tmux send-key -t $dirname:1.1 "clear" C-m
tmux send-key -t $dirname:1.2 "clear" C-m
tmux send-key -t $dirname:1.4 "clear" C-m
sleep 2
# Move focus to pane 3 - This should be the center pane
tmux send-key -t $dirname:1.3 "tmux select-pane -t 3" C-m
# tmux send-key -t $dirname:1.3 "clear" C-m # By default I don't clear the center pane to get the banner
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment