Automatically start tmux

This guide describes how to setup tmux to automatically launch as part of a user’s shell environment. If you don’t have tmux installed, you can use a package manager or follow my guide to compiling tmux. In order to launch tmux with the user’s shell session, we need to add a line to that user’s .bashrc file.

This command will open the .bashrc script for the current user in vim for editing.

vim ~/.bashrc

Now add the follow lines to the user’s .bashrc.

if [ `which tmux 2> /dev/null` -a -z "$TMUX" ]; then
    tmux -2 attach || tmux -2 new; exit

Now to break down what the above lines of code do:

if [ $TERM != "screen-256color" ] && [ $TERM != "screen" ];“, checks the $TERM environment variable to see if tmux is already running; in order to avoid an infinite loop of launching tmux.

tmux attach || tmux new; exit“, this launches tmux, launches tmux and attaches to any running session; if there are no sessions of tmux running then it creates a new one. exit, this closes the shell session running tmux when the last tmux window is closed.


  • 2012-12-11: Rewritten for clarity.
  • Trackback are closed
  • Comments (1)
    • Alexander D’hoore
    • Oct 29th. 2012 4:50am

    Does what it says..

Comment are closed.