Skip to content

Instantly share code, notes, and snippets.

@tomafro
Created August 13, 2009 17:17
Show Gist options
  • Save tomafro/167309 to your computer and use it in GitHub Desktop.
Save tomafro/167309 to your computer and use it in GitHub Desktop.
#compdef gem
# My first zsh completion function, for the gem command. It lets you type 'gem <tab>' to complete gem commands
# (including installed ones) and for some commands (currently just open and update) allows you to complete gem
# names as well. The implementation isn't ideal, so I'd appreciate advice on how I can improve it, particularly
# the 'caching' of the gem_commands and installed_gems.
_gem_commands () {
if [[ -z $gem_commands ]] ; then
gem_commands=$(gem help commands | grep '^ [a-z]' | cut -d " " -f 5)
fi
# This seems unnecessary, but if I try to set gem_commands to an array, it falls over.
typeset -a gem_command_array
gem_command_array=($(echo $gem_commands))
compadd $gem_command_array
}
_installed_gems () {
if [[ -z $installed_gems ]] ; then
installed_gems=($(gem list | grep '^[A-Za-z]' | cut -d " " -f 1))
fi
typeset -a installed_gem_array
installed_gem_array=($(echo $installed_gems))
compadd $installed_gem_array
}
if (( CURRENT == 2 )); then
_gem_commands
else
if [[ $words[2] == open || $words[2] == update ]] ; then
_installed_gems
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment