This one is simple but nifty.

I have a slew of aliases that launch various apps. Quite a few of them launch apps that I generally only use with one type of file. Xcode and TaskPaper, for example. I wanted a completion system that would find just the applicable files in the directory. In most cases with these apps there’s only one file per project, so being able to type xc and hit tab to open the xcodeproj file for the current project is handy.

Documentation on complete and compgen isn’t great, so it took me a while to figure out what should have been pretty simple. I got it, though, and I like the results.

Here’s the script. You’ll want to modify it to add the applications and aliases you want, but it’s simple enough that it shouldn’t take a lot of explanation. Add some aliases at the top (examples in the comments), build out case statements for them, and then make sure they’re all listed in the complete command at the bottom. Happy tabbing.

## Applications
# alias acorn="open -a Acorn"
# alias alpha="open -a ImageAlpha"
# alias tp="open -a TaskPaper"
# alias byword="open -a Byword"
# alias xc="open -a Xcode"
## subp is a custom function for finding Sublime projects

function _complete_app_alias()
	local cmd="${1##*/}"
	local word=${COMP_WORDS[COMP_CWORD]}
	local line=${COMP_LINE}
	local patt

	# Check to see what command is being executed.
	case "$cmd" in

	COMPREPLY=($(compgen -f -X "$patt" -- "${word}"))

complete -o bashdefault -o dirnames -o filenames -o default -o nospace -F _complete_app_alias sublp subp esp xc tp alpha byword mmdc || \
complete -o default -o dirnames -o filenames -o nospace -F _complete_app_alias sublp subp esp xc tp alpha byword mmdc

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