Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Personally I find the small shell function below really helps my tmux workflow. Run it with no arguments and you'll get a list of available sessions. Run it with a session name and it will start the session if it doesn't exist or reattach if it does. If you're already in one tmux session, it will just switch over your client to that session.

The function below will also automatically detach other clients, but for me it's what I want since other clients are always just me on another machine, often with a different screen size.

    tm () {
        if [ -z $1 ]
        then
            tmux list-sessions
            return
        fi
        tmux detach -s $1 2> /dev/null
        if [ -n "${TMUX+1}" ]
        then
            tmux switch-client -t $1 2> /dev/null || tmux new-session -s $1
        else
            tmux attach-session -t $1 2> /dev/null || tmux new-session -s $1
        fi
    }


I have a better version (in the sense that it automatically names sessions after the current directory):

  if [ -z "$1" ]; then
    name="$(basename $PWD)"
    name="${name//\./-}"
  else
    name=$1
  fi

  tmux has -t "=$name" && tmux attach -t "$name" && exit

  tmux new-session -d -s "$name" -n shell


Better is subjective. This version has no way to easily list sessions. Depends on what you want :)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: