Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have set up aliases for apt-get update, apt-get install, and so on. Thats quite convenient. But what I really would like to have is something like this:

type agi TAB vi and have it automatically list all packages starting with vi, as it would happen when issuing apt-get install TAB vi.

Would something like this be achievable somehow, maybe by writing a little script?

share|improve this question
add comment

2 Answers 2

up vote 4 down vote accepted

Bash programmable completion is something I haven't bothered much with myself, but I believe this will do what you want (NB assumes bash version 4.0 or newer. If you run Ubuntu 10.04 or newer, you're good.):

alias agi='apt-get install'
_agi_completion() { 
    mapfile -t COMPREPLY < <(apt-cache --no-generate pkgnames "${COMP_WORDS[COMP_CWORD]}")
}
complete -F _agi_completion agi

Programmable Completion in bash's manual explains how it works, though it's quite complex.

share|improve this answer
    
+1, it also works prepending sudo –  enzotib Jul 21 '11 at 5:33
    
uhm, I should correct my preceding comment: completion works with sudo prepended, but alias is not found, once you hit enter –  enzotib Jul 21 '11 at 7:57
    
@enzotib, yes, sudo has no knowledge of shell functions or aliases, only the shell deals with those. I'm guessing the alias is actually alias agi='sudo apt-get install' –  geirha Jul 21 '11 at 13:25
add comment

You need to add your aliases to the shell completion dictionaries so it knows what it is completing for those commands. The shell completions should already be named functions, but you have to tell it that they apply to your new command names.

share|improve this answer
2  
Could you please elaborate on this answer? Maybe add a step by step guide? –  N.N. Jul 20 '11 at 21:42
    
I only know step by step for zsh, not bash. Maybe it will put somebody on the right track that knows bash, otherwise I'll try to research it later. –  Caleb Jul 20 '11 at 21:47
    
I was trying to google shell completion dictionaries but could not find anything useful as a starting point. Is there a certain location in the fs where I could look for those dictionaries? –  elementz Jul 20 '11 at 21:54
    
Do you have a "bash-completions" package? If not look through the files included with the "bash" package and see if you can make out which one have completion definitions. –  Caleb Jul 20 '11 at 21:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.