Quicksilver-style completion in bash shell?
July 11, 2008 6:44 AM   Subscribe

How can I get Quicksilver-style completion in the bash shell?

Suppose I have a bunch of files in a directory. One of them is named 'metafilter'. I'd like to say cat mflr[TAB] and have that be expanded.

I think this is equivalent to asking for an implicit * between each typed character.

Anyone have bash-completion-fu?
posted by dmd to Computers & Internet (11 answers total) 4 users marked this as a favorite
 
It's not Quicksilver style, but plain old cat me[TAB] will do it. Or if not, cat mef[TAB] or as many chars as it takes to disambiguate.

You may already realize that. If so, I'm now watching this thread because I'm curious too. :)
posted by rokusan at 7:44 AM on July 11, 2008


Well, bash does have auto-complete features - not quite Quicksilver-style completion, but you could say get all the files in a metafilter director by explicitly using wildcards as allowed by bash: ls -la m*f*l*r. There's also neat autocompletes you can easily learn how to use with the tab and escape keys.
posted by grippycat at 7:48 AM on July 11, 2008


have you read this?

you'll probably need to modify the complete() function itself if you want this to be the universal behavior.
posted by azazello at 7:50 AM on July 11, 2008


Here is the bash reference manual's section on programmable completion. It looks like you could add something to your bashrc to force bash to use a function of your choice to evaluate completions, and you can restrict this to certain commands. This appears to be how the amazing'bash-completion' package does its magic. (available on mac OS through MacPorts and maybe Fink)
posted by mkb at 8:43 AM on July 11, 2008


Here's a quick and dirty one
#!/bin/bash

_radcompletion() {
    local resp=${2:0:1}
    for ((i=1; i<>
        resp=$resp'*'${2:i:1}
    done
    COMPREPLY=( $(compgen -f $resp) )
    return 0
}
complete -F _radcompletion zomg
Now if you type zomg mflr it will expand like you want. COMPREPLY probably depends on sourcing bash_completion first, I don't know. If you want this *everywhere* you'll need to put a wrapper around the complete function or replace all the registered completes.
posted by azazello at 9:25 AM on July 11, 2008


That sucked. Here it is in more readable form.
posted by azazello at 9:27 AM on July 11, 2008


It looks like a bit of alteration to the _filedir() function in /etc/bash_completion might do the trick - without needing a prefix.
posted by [@I][:+:][@I] at 9:34 AM on July 11, 2008


also you can add -d to the -f option for compgen to expand directories as well, and there's a bunch of others. and yes, you can probably put this into _filedir to have it everywhere.
posted by azazello at 9:38 AM on July 11, 2008


Best answer: This will overload the filedir function like [31d1] suggested but it fully expands the first match. I couldn't figure out how to make it stop expanding when there's more than one match.
posted by azazello at 10:16 AM on July 11, 2008 [1 favorite]


Best answer: Here it is in a permanent location.
posted by azazello at 7:29 AM on July 13, 2008


another completition tip: eliminate the need to hit the tab key twice when there is more than one match by placing this in ~/.inputrc

set show-all-if-ambiguous on
posted by namewithoutwords at 8:39 AM on July 13, 2008


« Older .htaccess files and groups   |   This is not my beautiful OS. Newer »
This thread is closed to new comments.