views:

348

answers:

3

Question: I have a question that is apparently not answered by this already-asked Bash completion question on Stack Overflow. The question is, how to get Bash alias completion (for any alias) on a partial substring.

Example: For example, assume I have the following aliases:

open.alicehome="cd /usr/home/alice"
open.bakerhome="cd /usr/home/baker"
open.charliehome="cd /usr/home/charlie"
gohomenow="shutdown -now"

I would like to lazily just type "baker{{TAB}}" to invoke the second alias.

I would like to lazily just type "home{{TAB}}" to get a list of all of the above aliases that I can then choose from with the keyboard (optimal) or choose by typing an unambiguous substring that distinguishes among the three options (less than optimal).

.. OR ..

I would like to lazily just type "home" and then repeatedly press {{TAB}} until the specific alias I want shows up, then press {{ENTER}} to execute it.

Feel free to be creative: If you have a way to do this, even if it requires resorting to extreme guru hackery, please feel free to share it and feel free to explain your guru hackery to the level of a five-year-old who will have to try to implement your idea for himself.

Links to existing web-pages or RTFMs are welcome.

A: 

Probably you can achieve what you want with custom bash_completion settings, but I'm not going to do the job for you, ok? :-)

If you don't know what I'm talking about, see here and here for details. By the way, I would find very annoying this

I would like to lazily just type "home" and then repeatedly press {{TAB}} until the specific alias I want shows up, then press {{ENTER}} to execute it.

which I think is the M$ style of "completion" (are you coming for DOS/windows environment?)

Davide
Check out EMACS icicles, it does this.
dreftymac
+1  A: 

If you really want to change bash's tab behavior,

# ~/.inputrc
# The default binding for [Tab] is ``complete'', which is what we're used to.
# ``menu-complete'' gives you irssi-like behavior: cycle through completions.
"\t": menu-complete

This is documented in The GNU Readline Library # Letting Readline Type For You.

ephemient
+1  A: 

You can generate the matches for a specified substring with compgen -c -X'!*substring*'

To include this in bash autocompletion you can create a .bash_completion file in your home directory containing something like this:

_comp() {
    local cur

    cur=${COMP_WORDS[$COMP_CWORD]}

    COMPREPLY=( $( compgen -c -X '!*'$cur'*' ) )

}
complete -F _comp $nospace eval

This match function will run when you press <TAB> on the arguments to the eval command. I haven't found a way to include a new matching function for the first command search in bash, I've only found documentation on how to add custom completions for a specific command, like eval above.

But I'm a bit skeptical to how useful this is... There is no easy way to select which match you want from the list.

Anders Westrup