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
…
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 commandtest
(which probably just immediately exits with a non-zero result). – Chris Johnsen – 2014-10-07T08:34:25.087I just tried to run commands in the terminal.
tmux-new -s test
doesn't do anything either. – firedev – 2014-10-09T03:32:15.520