It’s a common problem: All the “good” names for complex programms are already in use, so you end up with some long programm name like “systemctl”, which furthermore has lots of options that you really need, but usually you may either define an alias like “sctl” (then you would need to remember and type out all options) OR use bash’s autocomplete-feature (but type the full programm-name everytime).
Via Ubuntuforums, I came across this script that provides a wrapper for autocomplete:
# Author.: Ole J # Date...: 23.03.2008 # License: Whatever # Wraps a completion function # make-completion-wrapper # # 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" }
I preferred locating this script as “make_completion_wrapper” within “/usr/local/include/”. You may want to comment out the last two echo-functions as they are disturbing when running the script interactively.
For usage, let’s get back to the example in the beginning: I want sctl to be an alias of systemctl. This is what the corresponding part in “.bashrc” should look like:
<…> # First I need to include and run above script . /usr/local/include/make-completion-wrapper # and an alias I want have my completion for alias sctl="systemctl" # I found out that systemcontrol gets completed by the hook "_systemctl" # via "complete -p systemctl" (it' s the part behind -F in the output) # So let's create a new hook _sctl, which is a wrapper for _systemctl make-completion-wrapper _systemctl _sctl systemctl # Now, we need to bind the hook _sctl to the "command" sctl: complete -F _sctl sctl <…>
That’s it, you’ll have a neat alias with full-featured autocompletion!
Update 2016-02-26: Fixed error on line 11 of the .bashrc-snippet (I used make_completion_wrapper, but the function’s called make-completion-wrapper)