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
…