Skip to content

Instantly share code, notes, and snippets.

@okraits
Last active June 2, 2024 19:10
Show Gist options
  • Save okraits/c8f8ddabc353c0c947ce4cbb7f8d8a4c to your computer and use it in GitHub Desktop.
Save okraits/c8f8ddabc353c0c947ce4cbb7f8d8a4c to your computer and use it in GitHub Desktop.
Create or re-attach a tmux session for a given project path (and enter python venv and create additional tmux windows)
#!/bin/sh
# first argument: path to base directory
if [ -n "$1" ]; then
BASE_PATH="$1"
SESSION_NAME="$BASE_PATH"
else
echo "Required argument BASE_PATH missing."
exit 1
fi
cd "$BASE_PATH" || exit 1
# re-attach existing session or create new session
if tmux has-session -t "$SESSION_NAME"; then
tmux attach-session -d -t "$SESSION_NAME"
else
# try to find and activate virtual python environment
VENV="$(find . -name venv -type d)"
if [ -z "$VENV" ]; then
# try to find todo directory and open files
TODO="$(find .. -name todo -type d)"
if [ -z "$TODO" ]; then
tmux new-session -d -s "$SESSION_NAME" -n vim \
'nvim; zsh'
else
tmux new-session -d -s "$SESSION_NAME" -n vim \
"nvim \"$TODO\"/*; zsh"
fi
else
tmux new-session -d -s "$SESSION_NAME" -n vim \
"source \"$VENV/bin/activate\" && nvim; zsh"
fi
# second argument: do a horizontal split
if [ -n "$2" ] && [ "$2" = "split" ]; then
tmux split-window -d -h
fi
tmux new-window -d
tmux attach-session -t "$SESSION_NAME"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment