Last Updated: January 28, 2019
·
3.281K
· oleiade

Tmux : Named sessions with autocomplete

Tmux is a great tool, but I usually find it hard to track my sessions. So after a few googling I found a trick, which allows to name, and easily retrieve alive tmux sessions (with autocomplete)

Here's the function (zsh)

function tm() {
    [[ -z "$1" ]] && { echo "usage: tm <session>" >&2; return 1; }
    tmux has -t $1 && tmux attach -t $1 || tmux new -s $1
}

function __tmux-sessions() {
    local expl
    local -a sessions
    sessions=( ${${(f)"$(command tmux list-sessions)"}/:[ $'\t']##/:} )
    _describe -t sessions 'sessions' sessions "$@"
}
compdef __tmux-sessions tm

Example :

$ tm testsession1  # Launch a tmux session named testsession1
$ tm testsession2  # Launch a second tmux session named testsession2
$ tm <tab>  # List (autocomplete) existing sessions

Source