Automatically log into a tmux session

- 1 min read

Put this at the end of your shell configuration file (.bashrc or .zshrc)

if command -v tmux &> /dev/null && [ -n "$PS1" ] && [[ ! "$TERM" =~ screen ]] && [[ ! "$TERM" =~ tmux ]] && [ -z "$TMUX" ]; then
  if tmux list-sessions > /dev/null; then
    exec tmux attach
  else 
    exec tmux
  fi
fi

This snippet will attach to an existing session, or create a new one unless you are already inside a TMUX or screen session.