Start a new session from within tmux with ZSH_TMUX_AUTOSTART=true

2

1

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

Posted 2014-10-06T04:13:47.437

Reputation: 203

Answers

1

tmuxinator solved it:

tmuxinator start [project]

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

firedev

Posted 2014-10-06T04:13:47.437

Reputation: 203

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

Chris Johnsen

Posted 2014-10-06T04:13:47.437

Reputation: 31 786

Hmm, something doesn't work here. https://gist.github.com/firedev/204119182d482848bb71

– firedev – 2014-10-07T07:17:12.580

@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). – Chris Johnsen – 2014-10-07T08:34:25.087

I just tried to run commands in the terminal. tmux-new -s test doesn't do anything either. – firedev – 2014-10-09T03:32:15.520