tmux even easier: tm with fuzzy completion

You may have seen the “fuzzy” Bash completion I linked to in my up post yesterday. I applied the same technique to my [tm] utility for completing tmux session and window names.

I find this handy because if I type part of a name thinking I’m jumping to a session, but that session has ended, I end up creating a new session with a partial name. With completion, I can type my partial name and hit tab to be sure there will be a match before running.

If you have no arguments and hit tab, it will list all sessions. Typing any part of a session name will begin matching, e.g. j⇥ will expand to a session named “jekyll,” as will kl⇥. Any part of the name is valid for matching, and it’s case insensitive.

If you have a session in the arguments already, it will begin completing window names for that session, or listing all windows if you hit tab after a space following the session name. Window names are entirely optional in tm.

Yes, I cheated a little and defaulted to Ruby for some of the string handling, but I decided a while ago that there’s really no crime in that.

If you’re still reading and are a tmux user, I’ll assume you know what to do with this script. That’s fair, right?

tm.completion.bashraw
_tm_complete() {
	local rx
	local token=${COMP_WORDS[$COMP_CWORD]}
	local IFS=$'\t'
	local words
	if [ $COMP_CWORD -eq 2 ]; then
		words=$(tmux list-windows -t ${COMP_WORDS[1]} 2> /dev/null | awk '{print $2}' | tr -d '*-' | tr "\n" "\t")
	elif [ $COMP_CWORD -eq 1 ]; then
		words=$(tmux -q list-sessions 2> /dev/null | cut -f 1 -d ':' | tr "\n" "	")
	fi

	local nocasematchWasOff=0
	shopt nocasematch >/dev/null || nocasematchWasOff=1
	(( nocasematchWasOff )) && shopt -s nocasematch

	local w matches=()

	if [[ $token == "" ]]; then
		matches=($words)
	else
		for w in $words; do
			rx=$(ruby -e "print '$token'.gsub(/\s+/,'').split('').join('.*')")
			if [[ "$w" =~ $rx ]]; then
				matches+=($w)
			fi
		done
	fi

	(( nocasematchWasOff )) && shopt -u nocasematch

	COMPREPLY=("${matches[@]}")
}

complete -F _tm_complete tm

Ryan Irelan has produced a series of shell trick videos based on BrettTerpstra.com posts. Readers can get 10% off using the coupon code TERPSTRA.

Brett Terpstra

Brett is a writer and developer living in Minnesota, USA. You can follow him as ttscoff on Twitter, GitHub, and Mastodon. Keep up with this blog by subscribing in your favorite news reader.

This content is supported by readers like you.