Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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

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
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? – jottr 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

Your Answer


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.