"tmx" (tmux) erroneously connects to existing session because of prefix matching

I used "tmx" to create a tmux session called "foos" and now I want to create a new session called "foo", where the latter is a prefix of the former. But tmux connects me to the existing session instead.

I would like to create the specified session directly. Even better, I’d like to switch "tmx" away from prefix matching and to exact matching when working with sessions in all contexts.

My "tmx" script has a bunch of stuff in it, but I think the relevant part is:

  if tmux has-session -t "${name}" &> /dev/null; then
    exec env SSH_AUTH_SOCK="${d}/${s}" ${f} tmux "${args[@]}" attach-session -t "${name}"
  else
    exec env SSH_AUTH_SOCK="${d}/${s}" ${f} tmux "${args[@]}" new-session -s "${name}"
  fi
Asked By: Mihai Danila

||

From man 1 tmux:

target-session is tried as, in order:

  1. A session ID prefixed with a $.
  2. An exact name of a session (as listed by the list-sessions command).
  3. The start of a session name, for example mysess would match a session named mysession.
  4. An fnmatch(3) pattern which is matched against the session name.

So it is by design: if there is no exact match, less exact matching kicks in. Unless…

If the session name is prefixed with an =, only an exact match is accepted (so =mysess will only match exactly mysess, not mysession).

Solution: use -t "=${name}" where appropriate.

Answered By: Kamil Maciorowski
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.