TIL

tx

03.02.2023

I have recently been trying to do a proper setup of tmux and as a part of it i made this little shell function to make re-attaching sessions a bit easier. It will give fzf prompt with available sessions if called without a session name. On the other hand when called with a session name it will check if it exists and attach or create based on the result.

tx() {
if [ -z ${1+x} ]; then
session=$(tmux list-sessions | fzf | awk '{print $1}' | sed 's/:$//' )
tmux a -t $session
else
if tmux list-sessions | grep $1 2>&1 > /dev/null; then
tmux a -t $1
else
tmux new -s $1
fi
fi
}
_tx() {
sessions=$(tmux list-sessions 2>/dev/null)
if [[ -z "$1" ]]; then
echo $sessions | awk '{split($0,a,":"); print a[1]}'
else
echo $sessions | awk '{split($0,a,":"); print a[1]}' | grep --color=never $1
fi
}
complete -C _tx tx