So one approach is to spawn a new window and then attach:
tmux -S /tmp/socket new-window -n vi /usr/bin/vi
tmux -S /tmp/socket attach
So the client can create a new window and then connect to it.
However, I suspect that a tmux session might not be doing what you want it to do.
The reason that
attach has no command is because a session represents a view what is currently seen on the screen, which is shared between all connected clients. So attaching with a command is a little weird. All clients will view the same thing, and the
new-window call will change the view for all attached clients.
If you want to share a collection of windows (e.g views of running programs), but have each client have a distinct view then you need to create sessions. You can use the
-t argument to
new-session to make a session that shares its windows with another session. Apparently you can't use
-t together with a command for the new session, which is actually a valid use case, so you end up using the
-d flag and then spawning the command. To make things even more irritating it would appear that
new-window lacks a session argument, so you instead rely on the order that things are called!
tmux -S /tmp/socket new-session -s base # run once
# For each client
tmux -S /tmp/socket new-session -t client-view -d
tmux -S /tmp/socket new-window /usr/bin/vi
tmux -S /tmp/socket attach -t client-view
This is all a bit of kludge put I have managed to use this sort of approach effectively for a number of years. (Each of my terminal windows is a distinct view into a shared tmux session).
You might be interested in this script https://github.com/rainerborene/tmx/blob/master/tmx that wraps all of this up into a single shell script.