36

My current workflow is:

  1. CTRL+SHIFT+T to launch a new terminal window. That starts a new zsh terminal.
  2. Type tmux to start tmux.

How can I have tmux load by default with a new terminal window?

7 Answers 7

54

There are at least two ways:

  1. Write something like

    if [ "$TMUX" = "" ]; then tmux; fi
    

    at the beginning of ~/.zshrc. Note the conditional test to a possible loop when tmux spawns its own zsh.

  2. Modify terminal launching command to something like

    xterm -e tmux
    

I prefer the second way, because sometimes I need to launch a terminal without tmux (for example when I need to reconnect to an existing session).

8
  • 2
    Or have different shortcuts for xterm -e tmux and plain xterm. Jun 21, 2012 at 13:37
  • Yes, actually I meant it under "I prefer the second way" =)
    – rush
    Jun 21, 2012 at 13:39
  • 1
    Executing tmux at the end of .zshrc will cause an endless loop of zsh starting tmux starting zsh starting tmux ...
    – Thor
    Jun 21, 2012 at 14:56
  • 1
    How would one go about "modifying terminal launching command"?
    – eoinoc
    Jun 22, 2012 at 9:04
  • @eoinoc it depends on your environment. For example, I'm using awesome wm, therefore I just need to modify string in my config file. In kde you need to edit hotkeys. And so on.
    – rush
    Jun 22, 2012 at 9:11
22

There is actually a default plugin tmux for oh_my_zsh.

Add it to your plugins list then set ZSH_TMUX_AUTOSTART=true in your .zshrc

For more reference, go here

3
  • Out of curiosity are you using that now? Doesnt work for me. Have that env set and it doesnt start. tmux definitely added to plugin array,
    – RichieHH
    Aug 8, 2020 at 5:20
  • 2
    you have to add the ZSH...=... assignment before the line source $ZSH/oh-my-zsh.sh, from github.com/ohmyzsh/ohmyzsh/issues/3676#issuecomment-77806736
    – 3tbraden
    Feb 6, 2021 at 2:42
  • also add tmux to the list of your plugins in .zshrc: plugins=(... tmux)
    – starm3nace
    Jan 17, 2022 at 11:23
16

add it to your .zshrc

if [ -z "$TMUX" ]
then
    tmux attach -t TMUX || tmux new -s TMUX
fi

then tmux will automatically connect to a session called TMUX when you launch your terminal.

0
8

Be careful with the echo tmux >> ~/.zshrc solution though, I remember that simply throwing a bash in a .cshrc file caused me trouble over SSH.

IIRC the problem occurred with non-interactive shells, so you should test for that.

case $- in *i*)
  if [ -z "$TMUX" ]; then exec tmux; fi;;
esac
1
  • You're missing a space between " and ].
    – n.st
    Apr 9, 2014 at 1:17
1

My compromise is to automatically start tmux if no sessions are running.

This way, only the first terminal window starts tmux. A following windows do not start tmux and you can decide to attach to a session or to not use tmux at all there.

To do so add the following at the beginning of ~/.zshrc:

if [[ ! $(tmux list-sessions) ]]; then 
  tmux
fi
1

Add this in your ~/.zshrc:

if [ -z $TMUX ]; then; tmux -t TMUX || tmux new -s TMUX; fi
0

Add "tmux" to your .zshrc file, which gets executed every time you start zsh. The quick way:

echo tmux >> ~/.zshrc
1
  • 1
    The thing with this, is that it starts a new session in every new instance and it bloats tmux with sessions that you've never exited (also tmux will complain about it). Not a good solution.
    – Eksapsy
    May 12, 2019 at 18:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .