Skip to content

Instantly share code, notes, and snippets.

@akkerman
Created November 13, 2021 18:20
Show Gist options
  • Save akkerman/6a3e5b75398a6152b1478c2880409fa2 to your computer and use it in GitHub Desktop.
Save akkerman/6a3e5b75398a6152b1478c2880409fa2 to your computer and use it in GitHub Desktop.
start a tmux session in the current directory
#!/usr/bin/env bash
# start a tmux session in the current directory
dirname="$(basename $PWD)"
# tmux sessionname cannot contain a dot, replace with underscore
session="${dirname/./_}"
tmux_sessions=$(tmux ls -F '#{session_name}')
if [ -n "$TMUX" ]; then
# already in tmux
if [[ $tmux_sessions != *"$session"* ]]; then
# force create session, don't attach
TMUX='' tmux new-session -Ad -s "$session"
fi
tmux switch-client -t "$session"
else
# not in tmux
# create session, or attach when exists
tmux new-session -As "$session"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment