# Ruby command -- bash readline completion

**URL:** <https://www.ruby-forum.com/t/ruby-command-bash-readline-completion/51159>\
**Category:** Ruby\
**Created:** [December 27, 2005, 12:50pm UTC](https://www.ruby-forum.com/t/ruby-command-bash-readline-completion/51159 "2005-12-27T12:50:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [December 27, 2005, 12:50pm UTC](https://www.ruby-forum.com/t/ruby-command-bash-readline-completion/51159/1 "2005-12-27T12:50:07Z")

</div>

I have put together some basic bash readline completion for ruby.

- Typing `ruby -[TAB]` will give you a list of available command line  
switches.
- Typing `ruby -r[TAB]’ will give you a list of available libraries  
to load. For this I have disabled searching “.” because that could  
take a very long time.

See [[http://www.rubygarden.org/ruby?ProgrammableCompletition](http://www.rubygarden.org/ruby?ProgrammableCompletition)] for ri  
completion

Create a file named ruby in bash\_completion.d/ with –

#+++++++  
have ruby &&  
{

# Get a list of all available ruby libraries

\_ruby\_libraries()  
{  
COMPREPLY=( $( compgen -P “$prefix” -W “$( ruby -rfind -e ’  
libraries = []  
$:.each do |path|  
# searching “.” may take a very long time.  
unless path == “.”  
Find.find(path) do |found|  
Find.prune if File.dirname(found) =~ /test/  
libraries \<\< found.gsub(/^#{path}/,”“)[1…-4] if found[/.rb  
$/]  
end  
end  
end  
print libraries.join(” ")  
’  
) " – $cur ) )  
}

\_ruby()  
{  
local cur prev prefix temp

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

   # completing an option (may or may not be separated by a space)
   if [["$cur" == -?*]]; then
    temp=$cur
    prev=${temp:0:2}
    cur=${temp:2}
    prefix=$prev
   fi

   case "$prev" in
    -r)
      _ruby_libraries
      return 0
    ;;
   esac

   # handle case where first parameter is not a dash option
   if [$COMP_CWORD -eq 1] && [["$cur" != -*]]; then
    _filedir
    return 0
   fi

   # complete using basic options
   COMPREPLY=( $( compgen -W '-a -c -d -e -F -l -n -p -s -S -T -v

```

-w -W -0 -C -i -I -K -r’ – $cur ) )

```
   return 0

```

}  
complete -F \_ruby $default ruby  
}  
#+++++++

– Daniel
