Write a custom bash completion script for tmux attach

Revision history
Tags: tmux bash

I have a script I use to attach to existing, or create a new, tmux session. ~/bin/tat looks like the following

#!/bin/bash
# List existing tmux sessions, attach to existing, or create a new session
# stigok, feb 2019

if [ -z $1 ]; then
  tmux list-sessions
else
  tmux new -As $1
fi

Then I wanted to be able to tab-complete existing session names, instead of first having to write tmux ls (or tat) to get list of open sessions. This led to /etc/bash_completion.d/tat:

# stigok, feb 2019
_tat()
{
    local cur opts arglen
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"

    # All existing tmux session names
    opts="$(tmux ls -F '#{session_name}')"

    # The `tat` binary should only accept a single argument.
    # Only trigger completion on the first arg (after the binary name itself)
    arglen=${#COMP_WORDS[@]}
    if [ $arglen -eq 2 ]; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        return 0
    fi
}
complete -F _tat tat

Works beautifully. Now I wonder how I should package this in order to keep the completion code closer to the shell script itself…

References

If you have any comments or feedback, please send me an e-mail. (stig at stigok dotcom).

Did you find any typos, incorrect information, or have something to add? Then please propose a change to this post.

Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.