I’ve been in a fight with tmux on how to create new session under certain name, but if it already exists to attach to that current one for a very long time. Reason for that was that when I’m connecting to the remote servers I usually have my own session under certain name there and I’d like to attach to that existing one if it already exists.

Connecting to the server then issuing:

tmux list-sessions

and deciding upon that should I issue:

tmux new -s Ivan


tmux a -t Ivan

is tiresome, especially when there are many active sessions already.

Today, I finally found magic combination of switches by looking at tmux man page:

tmux new -As Ivan

This will create new session with name “Ivan”, but if that session already exists then -A switch will behave like an attach-session command, and end result will be attaching to the existing session under that name instead of creating new one or throwing an error.