Results 1 to 10 of 13

Thread: Bash completion of aliased commands

Threaded View

  1. #1
    Join Date
    Oct 2006
    Beans
    513

    Bash completion for aliased commands

    Problem: When you alias some command, custom completion no longer works.
    Eg.
    Code:
    alias agi='sudo apt-get install'
    agi fire<tab> # does not work
    This pretty much removes the advantage of having the alias in the first place. Especially when you want to install libgnome-settings-daemon-dev

    Solution:
    Most completers are defined as a bash function. My solution wraps the original completer. This obviously only works for function completers. (ie. complete -F).

    This should in principle work on any distribution with a recent bash and the completion pacackge. It's tested on feisty.

    make-completion-wrapper.sh :
    Code:
    # Author.: Ole J
    # Date...: 23.03.2008
    # License: Whatever
    
    # Wraps a completion function
    # make-completion-wrapper <actual completion function> <name of new func.>
    #                         <command name> <list supplied arguments>
    # eg.
    # 	alias agi='apt-get install'
    # 	make-completion-wrapper _apt_get _apt_get_install apt-get install
    # defines a function called _apt_get_install (that's $2) that will complete
    # the 'agi' alias. (complete -F _apt_get_install agi)
    #
    function make-completion-wrapper () {
    	local function_name="$2"
    	local arg_count=$(($#-3))
    	local comp_function_name="$1"
    	shift 2
    	local function="
    function $function_name {
    	((COMP_CWORD+=$arg_count))
    	COMP_WORDS=( "$@" \${COMP_WORDS[@]:1} )
    	"$comp_function_name"
    	return 0
    }"
    	eval "$function"
    	echo $function_name
    	echo "$function"
    }
    This will wrap a completer, adjusting the aliased arguments. Save it in a file called make-completion-wrapper.sh or add it to your ~/.bashrc file

    To find out what the completer function is called:
    Open a terminal and type:
    Code:
    complete -p <command name>
    Example:
    Code:
    $ complete -p apt-get
    complete -o filenames -F _apt_get apt-get
    $ complete -p oocalc 
    complete -o filenames -d -X '.[^./]*' -F _ooexp_ oocalc
    The blue output is the name of the completer function, and the red is extra information. If -F is missing you can not use this method to wrap the completer.

    As an example of usage lets add completion to alias agi='apt-get install' and alias acsh='apt-cache show'
    Add this to your ~/.bashrc :
    Code:
    # ...
    . /home/me/path-to-make-completion-wrapper.sh # or the content of make-completion-wrapper.sh
    # aliases and completion code:
    alias agi='sudo apt-get install'
    make-completion-wrapper _apt_get _agi apt-get install
    complete -o filenames -F _agi agi
    alias acsh='apt-cache show'
    make-completion-wrapper _apt_cache _acsh apt-cache show
    complete -o filenames -F _acsh acsh
    - Note that we've duplicted the red output from complete -p.
    - _agi and _acsh are arbitrary names, but to avoid name crashes, you should pick something distinct.

    If you just want to test if it works, paste the above code (substitute path to make-completion-wrapper.sh first) in a terminal and try to type
    Code:
    agi fire<tab><tab>
    Just remove the changes in your ~/.bashrc if you want to remove the completion or if something went wrong.

    Related information
    http://ubuntuforums.org/showthread.p...ash+completion
    http://www.debian-administration.org/articles/316
    http://www.debian-administration.org/articles/317

    PS: It shoud be possible to completely automate this process by parsing the output from complete
    Last edited by olejorgen; June 3rd, 2009 at 10:22 PM.
    Donation links
    Free hardware
    Petition for free drivers
    If every forum member donated $1 to FSF, they would almost double their income

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •