Having tmux load by default when a zsh terminal is launched

40,942

Solution 1

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).

Solution 2

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

Solution 3

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.

Solution 4

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
Share:
40,942

Related videos on Youtube

eoinoc
Author by

eoinoc

Updated on September 18, 2022

Comments

  • eoinoc
    eoinoc over 1 year

    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?

  • krasnoff
    krasnoff almost 12 years
    Or have different shortcuts for xterm -e tmux and plain xterm.
  • rush
    rush almost 12 years
    Yes, actually I meant it under "I prefer the second way" =)
  • eoinoc
    eoinoc almost 12 years
    How would one go about "modifying terminal launching command"?
  • rush
    rush almost 12 years
    @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.
  • eoinoc
    eoinoc almost 12 years
    @Rush Great, I was able to do it with gnome-conf unix.stackexchange.com/questions/41442/…
  • n.st
    n.st about 10 years
    You're missing a space between " and ].
  • x1a0
    x1a0 almost 8 years
    I am really sorry that I down-voted this answer by a misclick - would be nice if down vote can to removed/reset.
  • liz14
    liz14 about 5 years
    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.
  • RichieHH
    RichieHH almost 4 years
    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,
  • pfincent
    pfincent over 3 years
    In my setup, the $TMUX variable is not empty even if I'm not inside a tmux session. Executing echo $TMUX returns /tmp//tmux-1000/default,1647,2. Does anybody know why that is or how to adjust the script to make @rush's solution work under these circumstances?
  • tbraden
    tbraden over 3 years
    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
  • starm3nace
    starm3nace over 2 years
    also add tmux to the list of your plugins in .zshrc: plugins=(... tmux)