Tmux - autostart sessions

if (command -v tmux>/dev/null) && \
  [[ ! $TERM =~ screen ]] && \
  [ -z $TMUX ] && \
  [ "$TMUX_ENABLED" == "yes" ]; then
    /usr/bin/env tmux new -s 0
    /usr/bin/env tmux attach -t 0
fi

Breakdown:

  • (command -v tmux>/dev/null) - Tmux is installed
  • [[ ! $TERM =~ screen ]] && [ -z $TMUX ] - try not to run Tmux inside Tmux
  • [ "$TMUX_ENABLED" == "yes" ] - TMUX_ENABLED is set from Alacritty config file and it will start Tmux only in this terminal emulator.
  • tmux new -s 0 - find or duplicate a session with name 0
  • tmux attach -t 0 - attach to session 0