# Bash completion for the gem command

**URL:** <https://www.ruby-forum.com/t/bash-completion-for-the-gem-command/86654>\
**Category:** Ruby\
**Created:** [February 24, 2007, 2:53am UTC](https://www.ruby-forum.com/t/bash-completion-for-the-gem-command/86654 "2007-02-24T02:53:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [February 24, 2007, 2:53am UTC](https://www.ruby-forum.com/t/bash-completion-for-the-gem-command/86654/1 "2007-02-24T02:53:27Z")

</div>

To my surprise there apparently hasn’t been support for bash command  
line completion for the gem command. I’ve cobbled together a file that  
add just that. See

[http://schuerig.de/michael/blog/index.php/2007/02/24/gem-bash-completion/](http://schuerig.de/michael/blog/index.php/2007/02/24/gem-bash-completion/)

I’ve only tested it on Debian/Linux, on other Linux/Unix variants things  
might work somewhat differently. Also, I don’t even try to do  
completion for gem names; doing that for remote gems would be much to  
slow and I haven’t looked into getting that information from the local  
source\_cache.

Michael

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [February 26, 2007, 8:42pm UTC](https://www.ruby-forum.com/t/bash-completion-for-the-gem-command/86654/2 "2007-02-26T20:42:51Z")

</div>

- Michael S. (removed\_email\_address@domain.invalid) wrote:

> source\_cache.  
> I have this in my tcsh completions.rc, it provides basic command  
> completion and completion for installed gems:

set gemcommands=(build cert check cleanup repository contents   
dependency environment help install list outdated pristine   
query rdoc search sources specification uninstall unpack   
update repository)  
complete gem ‘p/1/$gemcommands/’ ‘n/help/$gemcommands/’   
‘p%_%D:/usr/local/lib/ruby/gems/1.8/gems/% %’ 'n/_/f:\*/’

I haven’t bothered with option completion, but of course that can be  
added with a similar effort to yours.

Remote gems should indeed be doable; it would mostly need a fast command  
to dump a list of gem names, or have something write them to a text file  
beforehand. If the full list is too big for that to be reasonable, the  
plaintext index could be split by prefix.

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [March 3, 2007, 1:33pm UTC](https://www.ruby-forum.com/t/bash-completion-for-the-gem-command/86654/3 "2007-03-03T13:33:40Z")

</div>

On 2/24/07, Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> slow and I haven’t looked into getting that information from the local  
> source\_cache.
> 
> Michael

Hi Michael,

Thanks for the script.

I’ve had a go at hacking in gem name completion for install and  
uninstall. For uninstall it uses installed names, for install it uses  
the names in the source cache. Only tested with gem 0.9.1.

Regards,  
George.

* * *

# Michael S., removed\_email\_address@domain.invalid, 2007-02-24

# George O., removed\_email\_address@domain.invalid, 2007-03-01

# Free for all uses.

GEM\_CACHE\_FILE=`ruby -rubygems -rrubygems/source_info_cache \ -e 'print Gem::SourceInfoCache.new.cache_file'`  
GEM\_AVAILABLE\_FILE=`dirname $GEM_CACHE_FILE`/source\_cache\_names

# 

# Output all available gem names.

# 

function available\_gems {  
if [ ! -e $GEM\_AVAILABLE\_FILE -o   
$GEM\_CACHE\_FILE -nt $GEM\_AVAILABLE\_FILE ]; then  
ruby \< $GEM\_AVAILABLE\_FILE  
require ‘rubygems/source\_info\_cache\_entry’  
text = File.read(‘$GEM\_CACHE\_FILE’)  
names = Marshal.load(text).values.map do |entry|  
entry.source\_index.latest\_specs.keys  
end.flatten  
puts names.uniq  
EOF  
fi

```
cat $GEM_AVAILABLE_FILE

```

}

# 

# Output all installed gem names.

# 

function installed\_gems {  
ruby \<\<EOF  
require ‘rubygems’  
names = Gem::SourceIndex.from\_installed\_gems.map do |name, spec|  
spec.name  
end.uniq  
puts names  
EOF  
}

\_gem()  
{  
local cur prev completions

```
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}

COMMANDS='build cert check cleanup contents dependency\
  environment help install list query rdoc search specification\
  uninstall unpack update'

COMMON_OPTIONS='\
  --source
  -p --http-proxy --no-http-proxy\
  -h --help\
  --config-file\
  --backtrace\
  --debug'

CERT_OPTIONS='\
  -a -add\
  -l --list\
  -r --remove\
  -b --build\
  -C --certificate\
  -K --private-key\
  -s --sign'

CHECK_OPTIONS='\
  -v --verify\
  -a --alien\
  -t --test\
  -V --version'

CLEANUP_OPTIONS='\
  -d --dry-run'

CONTENTS_OPTIONS='\
  -l --list\
  -V --version\
  -s --spec-dir\
  -v --verbose'

DEPENDENCY_OPTIONS='\
  -v --version\
  -r --reverse-dependencies --no-reverse-dependencies\
  -p --pipe'

HELP_OPTIONS=$COMMANDS

INSTALL_OPTIONS='\
  -v --version\
  -l --local\
  -r --remote\
  -b --both\
  -i --install-dir\
  -B --build-root\
  -d --rdoc --no-rdoc\
  --ri --no-ri\
  -f --force --no-force\
  -t --test --no-test\
  -w --wrappers --no-wrappers\
  -P --trust-policy\
  --ignore-dependencies\
  -y --include-dependencies'

LIST_OPTIONS='\
  -d --details --no-details\
  -l --local\
  -r --remote\
  -b --both'

QUERY_OPTIONS='\
  -n --name-matches\
  -d --details --no-details\
  -l --local\
  -r --remote\
  -b --both'

RDOC_OPTIONS='\
  --all\
  --rdoc --no-rdoc\
  --ri --no-ri\
  -v --version'

SEARCH_OPTIONS='\
  -d --details --no-details\
  -l --local\
  -r --remote\
  -b --both'

SPECIFICATION_OPTIONS='\
  -v --version\
  -l --local\
  -r --remote\
  -b --both\
  -all'

UNINSTALL_OPTIONS='\
  -a --all --no-all\
  -i --ignore-dependencies --no-ignore-dependencies\
  -x --executables --no-executables\
  -v --version'

UNPACK_OPTIONS='\
  -v --version'

UPDATE_OPTIONS='\
  -i --install-dir\
  -B --build-root\
  -d --rdoc --no-rdoc\
  -ri --no-ri\
  -f --force --no-force\
  -t --test --no-test\
  -w --wrappers --no-wrappers\
  -P --trust-policy\
  --ignore-dependencies\
  -y --include-dependencies\
  --system'

case "${prev}" in
  build)
    completions="$COMMON_OPTIONS $BUILD_OPTIONS"
    ;;
  cert)
    completions="$COMMON_OPTIONS $CERT_OPTIONS"
    ;;
  check)
    completions="$COMMON_OPTIONS $CHECK_OPTIONS"
    ;;
  cleanup)
    completions="$COMMON_OPTIONS $CLEANUP_OPTIONS"
    ;;
  contents)
    completions="$COMMON_OPTIONS $CONTENTS_OPTIONS"
    ;;
  dependency)
    completions="$COMMON_OPTIONS $DEPENDENCY_OPTIONS"
    ;;
  environment)
    completions="$COMMON_OPTIONS $ENVIRONMENT_OPTIONS"
    ;;
  help)
    completions="$COMMON_OPTIONS $HELP_OPTIONS"
    ;;
  install)
    completions="$COMMON_OPTIONS $INSTALL_OPTIONS `available_gems`"
    ;;
  list)
    completions="$COMMON_OPTIONS $LIST_OPTIONS"
    ;;
  query)
    completions="$COMMON_OPTIONS $QUERY_OPTIONS"
    ;;
  rdoc)
    completions="$COMMON_OPTIONS $RDOC_OPTIONS"
    ;;
  search)
    completions="$COMMON_OPTIONS $SEARCH_OPTIONS"
    ;;
  specification)
    completions="$COMMON_OPTIONS $SPECIFICATION_OPTIONS"
    ;;
  uninstall)
    completions="$COMMON_OPTIONS $UNINSTALL_OPTIONS 

```

`installed_gems`"  
;;  
unpack)  
completions=“$COMMON\_OPTIONS $UNPACK\_OPTIONS”  
;;  
update)  
completions=“$COMMON\_OPTIONS $UPDATE\_OPTIONS”  
;;  
\*)  
completions=“$COMMANDS $COMMON\_OPTIONS”  
;;  
esac

```
COMPREPLY=( $( compgen -W "$completions" -- $cur ))
return 0

```

}

[-n “${have:-}”] && complete -F \_gem $filenames gem

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [March 5, 2007, 10:24pm UTC](https://www.ruby-forum.com/t/bash-completion-for-the-gem-command/86654/4 "2007-03-05T22:24:59Z")

</div>

On Mar 3, 2007, at 04:33, George O. wrote:

> > I’ve only tested it on Debian/Linux, on other Linux/Unix variants
> 
> # 
> 
> EOF  
> fi
> 
> cat $GEM\_AVAILABLE\_FILE  
> }

RubyGems needs a way to get the source\_cache information without  
updating 😕 I should file myself a bug.
