> _ 

c u t u p @ c u t u p . o r g

 xml 

[bash fink completion] last modified: 03/25/2007 02:01 pm

A recent hint at macoshints shows how to create a shell script that will make all the Applications on your system available with tab completion. I've gotten used to Quicksilver and was interested in how this replicates some of that idea in the shell. I started poking around and ended up doing this another way.

Apparently, programmable completion has been around in bash for a while, but it's not really scripted by default. However, there are scripts and packages floating around to demonstrate it's capabilities. I like fink, and sure enough, fink has a bash-completion package. Here's what I had to do to get it working: Installed the bash-completion package from fink:

fink install bash-completion

Added the following lines to my .bashrc file:

source /sw/etc/bash_completion 
bind '"\t":menu-complete'

The first line calls the bash-completion script that fink installed (make sure you source it after any lines that affect PATH's), and the second line enable single-tab completions. This makes a Tab display the first possible completion, another Tab display the next, and so on, instead of the default double-tab-ask-first list.

Once this is turned on, it not only completes commands, but it can complete gui apps

open -a (TAB)

including ones it finds in Developer (it found xcode), complete ssh and scp (from known hosts), like

ssh [username@](TAB)

and man pages the same way, and etc ...

I wish there was a man file to enumerate more of what it does, but I learned that this all leverages the shell builtin complete. And looking in the /sw/etc/bash_completion file, we can see which of those it uses, and how. Neat stuff like directory commands only see directories, user commands only see users, network interface completion, man completion ... theres A LOT in there!

Theres also a /sw/etc/bash_completion.d/ folder that contains some more scripts, most of them seem to be custom for mac, like open.sh and fink.sh. I looked at the open script and right at the top is the search path. It was trivially easy to add extra places for it to look for apps. I added my games folder where I keep some apps separate. Seems easy enough to add more scripts too.

It's crazy, my Terminal just got a whole lot smarter, and it will take me a while to catch up. Unix heads always had all this stuff ages ago, but sometimes I guess they forget to let us know.

UPDATE: Debian seems to include the bash completion scripts in its base install, all you have to do is uncomment a couple of lines in the default .bashrc to get it running. And it's available as a package in cygwin as well.




[All Posts] [top]

tags:

bash
completion
fink