Start a new session from within tmux with ZSH_TMUX_AUTOSTART=true

5,816

Solution 1

tmuxinator solved it:

tmuxinator start [project]

And it breaks through the current tmux session straight into the new one.

Solution 2

Without -d, new-session will automatically attach to the new session (you probably do not want nested sessions*, thus the warning). What you probably want to do is create a new “detached” session and then switch the current client to that new session. I do not think new-session has an option to handle this use case, but it is fairly easy to do with a bit of shell scripting:

tmux-new() {
  if [[ -n $TMUX ]]; then
    tmux switch-client -t "$(TMUX= tmux -S "${TMUX%,*,*}" new-session -dP "$@")"
  else
    tmux new-session "$@"
  fi
}

# example usage:
tmux-new -s new

If there is no TMUX, then just run tmux new …. Otherwise,

  • unset TMUX to avoid the error while using -S to point to the current server,
  • add -d to create a detached session, and
  • add -P to print out the name of the new session (this requires tmux 1.8 or later).

The output (the new session’s name) is then given to switch-client.

If you do not have tmux 1.8 you might have to rework it a bit (e.g. make the function take a mandatory argument that names the new session so that you can pass it to both new-session and switch-client).

* You would have two status bars; it is complicated to type tmux commands to the inner session; and it may do odd things if you switch the inner client to view the outer session (think: hall of mirrors).
† Maybe the warning should be suppressed when using -d

Share:
5,816

Related videos on Youtube

firedev
Author by

firedev

Full-stack designer. Specializing in crafting components systems tailored to use cases on hand. Carefully stitching together back and front ends for improved user experience.

Updated on September 18, 2022

Comments

  • firedev
    firedev over 1 year

    I have enabled tmux plugin in oh-my-zsh with ZSH_TMUX_AUTOSTART=true and now every time I open terminal I am attached to a session. That part is really nice.

    The problem is – now I can't start a new session and get access to shell.

    Is there a way to start a new session without disabling tmux plugi?

    $ tmux new-session -s new
    sessions should be nested with care, unset $TMUX to force
    

    I have openend a ticket on github: https://github.com/robbyrussell/oh-my-zsh/issues/3192

  • firedev
    firedev over 9 years
    Hmm, something doesn't work here. gist.github.com/firedev/204119182d482848bb71
  • Chris Johnsen
    Chris Johnsen over 9 years
    @Nick: Er, only the first of those gisted commands uses uses the function. It probably needs a -s in there (i.e. tmux-new -s test), otherwise you are just starting (and switching to) a session with a window that runs the command test (which probably just immediately exits with a non-zero result).
  • firedev
    firedev over 9 years
    I just tried to run commands in the terminal. tmux-new -s test doesn't do anything either.