Blog

Tab Completion 101

I live and die by tab completion on the command line. Normally I use it to quickly navigate to deep directory structures. Rather than typing in the full path to something like "~/tmp/minneapolis/bookstore/grails-app/domain", I type "cd ~/tmp/min[tab]/b[tab]/g[tab]a[tab]/d[tab]".

Now that I spend most of my time in TextMate doing Groovy and Grails development, that little tab key is getting a workout. TextMate offers bundles for both Groovy and Grails that allow you to tab-complete your source code. Once the bundles are installed, I type "hm[tab]" instead of "static hasMany = [items:ClassName]". Note that this isn't really the same as the ctrl+space code completion you get from the Groovy/Eclipse plugin -- TextMate bundles just dump boilerplate code to the screen. But, quite honestly, I find myself spending less and less time in a full-blown IDE these days. IntelliJ still gets fired up for my increasingly rarer Java development, but the agile little TextMate editor is closer in spirit to the agile little Groovy language. (I am, however, eagerly awaiting IntelliJ 7 and its strong new support for Groovy and Grails. Since Groovy and Java mix together so well, I have ctrl+space envy whenever I'm working on a mixed project.)

Recently, Doyle of DoyleCentral.com posted an entry titled "Grails Tips and Tricks" that contained this intriguing little snippet: "If your [sic] on NIX and use bash/dash or some variant I have created an auto-complete feature that is quite handy for the command line."

Now, it seems, I can tab complete grails commands like "grails create-domain-class". You have no idea how happy this makes me. (You have no idea how happy this makes my students -- "grails crate^H^H^Hcrete^H^H^Hcreate-dmain"... you get the idea.) I now type "grai[tab] cr[tab]d".

So, here are the step-by-steps to get bash-completion running on your Mac.

Step #1: Buy a Mac

(That joke just gets funnier the more I tell it...)

Step #2: Install MacPorts

MacPorts is an apt-get-like tool for OS X. (Apt-get for Ubuntu, port for OS X -- easy breezy.) It allows you to quickly install Macintosh ports of popular Linux/Unix utilities. Utilities like...

Step #3: Install bash-completion

...bash-completion. Typing "port info bash-completion" says it all: "Brings programmable completion to bash like those pesky zsh users have had for years." Type "sudo port install bash-completion" to install it.

Step #4: Tweak Your .bash_profile

In your home directory, there is file called .bash_profile. This is where you can put all sorts of interesting little nuggets like environment variables and aliases. One line is "alias m=mate", so that I can type "m ." to pull up an entire directory of source code in TextMate. Another block of code looks like this:

### Grails
GRAILS_HOME=/opt/grails
PATH=$PATH:$GRAILS_HOME/bin
export GRAILS_HOME PATH

This, of course, is what allows me type in "grails" at the command prompt. BTW, I keep multiple versions of Grails in /opt using their fully-qualified directory names: grails-0.3.1, grails-0.4.1, grails-0.4.2. I then symlink "grails" the version I want to make active: "ln -s grails-0.4.2 grails". (I use the same trick for Groovy, Java, Ant, Tomcat, JBoss, Geronimo... it allows me to keep the environment variables static and flip them based on a single command-line tweak.)

To enable bash-completion, copy this block into .bash_profile:

### BASH Completion
if [ -f /opt/local/etc/bash_completion ]; then
    . /opt/local/etc/bash_completion
fi

To make sure that these changes take effect, type "source .bash_profile" or simply exit the terminal window and fire up a freshy.

Step #5: Download and Scatter the Grails Bits

Doyle posted a tarball that contains two magic files. Copy grails_commands to /opt/local/etc/bash_completion.d/. Copy command-list to $GRAILS_HOME.

Step #6: Enter Grails Nirvana

Here we go...

  • cd tmp
  • grai[tab] cr[tab]a[tab] bookstore (grails create-app bookstore)
  • cd b[tab] (cd bookstore)
  • grai[tab] cr[tab]d[tab] book (grails create-domain-class book)
  • m . (mate .)
  • add the interesting bits to /grails-app/domain/Book.groovy
  • grai[tab] r[tab]a[tab] (grails run-app)
  • visit "http://localhost:8080/bookstore" in the web browser of your choice

That, my friends, can only be called tabalicious.

Posted on Fri, 6 Apr 2007 11:39 by default (1067 day(s) old)

April 2007
Sun Mon Tue Wed Thu Fri Sat 
<  Mar | Apr |  May  >
1234567
891011121314
15161718192021
22232425262728
2930     
       
About:

E-mail: Scott Davis

Categories:
Syndication:

XML RSS ATOM

Powered by blojsom