Skip to content

Instantly share code, notes, and snippets.

@foca
Created April 25, 2012 12:11
Show Gist options
  • Save foca/2489279 to your computer and use it in GitHub Desktop.
Save foca/2489279 to your computer and use it in GitHub Desktop.
cdgem is a small bash utility function to cd into a rubygem's directory
# cd into matching gem directory ("cd -" friendly)
#
# cdgem noko # will cd into the first gem matching noko
# cdgem ^rails- # will cd into the first gem starting with "rails-"
# vim $(cdgem noko) # will open the gem's directory in vim
cdgem() {
local gempath=$(gem env gemdir)/gems;
local gem=$(ls "$gempath" | grep "$1" | sort | tail -1);
if [ -z "$1" ]; then
[ -t 1 ] && cd "$gempath" || echo "$gempath";
elif [ -n "$gem" ]; then
[ -t 1 ] && cd "$gempath/$gem" || echo "$gempath/$gem";
else
echo "Could not find gem \"$1\" in \"$gempath\"" 1>&2;
return 1;
fi
}
@jechol
Copy link

jechol commented Apr 14, 2015

Very useful. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment