Welcome to Deadman.org.


Creative Commons License

Saturday Oct 2 10:45am 2004
by Deadman

I finally got around to getting auto-complete for my bash aliases working. It was much easier than I thought, but mostly because I aped a lot of it. The first thing I stole from /etc/bash_completion (Debian's version, if that matters,) and I stole it quite a while ago, was the "have" function from /etc/bash_completion :

have() {                                                                                                                                  
    unset -v have                                                                                                                         
    type $1 >/dev/null 2>&1 && have="yes"                                                                                                 
}
#EG usage:
have vim && alias vi='vim -X'

I use that for all sorts of stuff because I use my bash environment across 3 operating systems and two Linux variants. Using have() makes it easy to alias vi to "vim -X" and not get screwed if there's no vim to be had.

In hunting around /etc/bash_completion, and trying to figure out what was going on via the bash man page, I saw that they were using a function to complete apt-get. I had a few aliases for apt-get that I'm very used to typing that only required (a) package name(s): acs, acS and agi. Alas, the function didn't work for them because they already had the apt "command argument" (for lack of a better term) in them. So I made "ac" and "ag" below.

Once I actually read _apt_cache it was quite simple to ape out the parts that seemed like they were blocking acs and friends.

have apt-get &&.                                                                                                                              
function _agi(){                                                                                                                              
    local cur prev                                                                                                                            
    COMPREPLY=()                                                                                                                              
    cur=${COMP_WORDS[COMP_CWORD]}                                                                                                             
    prev=${COMP_WORDS[COMP_CWORD-1]}                                                                                                          
    COMPREPLY=( $( apt-cache pkgnames $cur 2> /dev/null ) )  
    return 0                                                                                
}
if have apt-get ; then                                                                                                                            
    alias ac='apt-cache'                                                                                                                          
    complete -F _apt_cache $filenames ac   
                                                                                                       
    alias ag='apt-get'                                                                                                                            
    complete -F _apt_get $filenames ag 
                                                                                                           
    alias acs='apt-cache search'                                                                                                                  
    alias acS='apt-cache show'                                                                                                                    
    alias agi='apt-get install'                                                                                                                   
    complete -F _agi $filenames agi acs acS         
                                                                                              
    alias dist='apt-get -u dist-upgrade'                                                                                                          
    alias ug='apt-get -u upgrade'                                                                                                                 
    alias agu='ug'                                                                                                                                
    alias ud='apt-get update'                                                                                                                     
fi
Home

© 2007 Sam Rowe. Some rights reserved.
RSS headlines