Thursday, November 11, 2004

bash programmable completion

Last week, I was trying to get my directories all set up in a new development environment, and I ended up with a really deep nesting of directories to get to my actual code. After organizing everything in a standard web application source tree way, with java source code in directories based on the package, and packages named with the standard Sun-recommended naming convention, the path to one of my servlets looked something like this:

/home/spugbrap/projects/appName/src/com/baz/\
bar/foo/servlet/MyServlet.java

I created some soft links to get to specific places within the tree, but sometimes traversing the tree is necessary/useful. Most of the directories in the package hierarchy are *basically* empty except for a single subdirectory, so tab completion should come in handy.

So, to get to that servlet directory from my home directory, I was doing something like this ( indicates where I attempted filename completion by hitting the tab key):

15:04:52 Thu Nov 11 [~]
$ cd pro/app/src/

uh oh. already ran into a problem. When I said the directories in the package hierarchy are *basically* empty, I didn't mention they contain 1 subdirectory to get to the next level in the package hierarchy, as well as a CVS directory since this is all version controlled. So, when I got to the "src" directory, tab completion was not as useful as I had hoped. I needed a way to ignore the CVS directories when performing completion, so I could just do this to get to my final destination:

15:04:52 Thu Nov 11 [~]
$ cd pro/app/src/////se

So, I googled for a while, finding lots of information on how easy this is to do with "zsh", or on Mac OS X (I think), but was not finding anything about doing it with "bash", particularly in cygwin (although I figured that part shouldn't be relevant).

Eventually, I stumbled across a project that allows you to programmatically tell bash exactly how to complete things, even to the point of completing differently based on the command you're planning to execute, including completing entries from your ~/.ssh/known_hosts file if your command-line starts with "ssh" when you hit tab. This did exactly what I wanted, and much more, so now my directory navigation is much easier. I still use my soft links most of the time, but when I need to actually traverse the tree manually, I can do it in a minimal number of keystrokes.

Here's a link to this bash completion project on freshmeat:
http://freshmeat.net/projects/bashcompletion/

Enjoy!
-dave

Wednesday, November 03, 2004

Purpose of this blog

I noticed recently that I google for programming/utility/OS/etc-related information all the time. I mean, I've known this for a while... I think I probably do at least 10-50 google searches a day on average. If they changed to a pay service at some point (can't see that happening, but I've pondered it), I'd probably get depressed and resort to (gasp) books and real people for information.

But what finally dawned on me is that the reason I google so much is because I always assume that someone, somewhere has had the same question or problem as me, someone else has had a solution for them, and that conversation is out there somewhere, waiting for me to find, as soon as I can perfect my search terms and wade through enough results. Sometimes finding that information is quick and easy, other times it may take hours and still not turn up exactly what I need. Sometimes I might find the solution elsewhere in the end. Okay, so my problem is solved. Now what?

Sure, I often add a personal bookmark (which promptly gets lost in a sea of unsorted bookmarks, never to be seen again until some day when it ends up being a bookmark to a 404 page). I've even been making small notes in an ongoing text file for the past couple years. Sometimes I'll even IM a buddy to share something I find particularly enlightening.

But what about the next person who makes the same assumption that I made, that somewhere out there on the web, that information can be found? I'd like to make their search as quick and simple as other people have often made mine, by sharing the information I find, however obscure it might be.

So, I'm going to start posting some of this stuff here instead.

It's not meant to be particularly pretty or grammatically correct, and it might not make sense or be useful to anyone else in the world. But I have a feeling some of it will be of use to someone, someday.

I will not claim to be an expert on anything in particular, in fact most of what I post here will probably be things I just learned about for the first time, so I welcome and encourage corrections, comments, alternative solutions, additional sources of information, questions, etc.

-spugbrap