Skip to content

Instantly share code, notes, and snippets.

@hardfire
Created April 9, 2017 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hardfire/b967b67083a2c73cfc8d89ebcea8b981 to your computer and use it in GitHub Desktop.
Save hardfire/b967b67083a2c73cfc8d89ebcea8b981 to your computer and use it in GitHub Desktop.
HOWTO: autocompletion in a bash function/alias
# I have a function called as shocker which 'sh-es' into a docker container
# to enable autocompletion in the command, I do the following
# `complete -F __docker_complete_container_names shocker`
# __docker_complete_container_names is a function available in /etc/bash_completion.d/docker
# the above file is available at https://docs.docker.com/machine/completion/
# the __docker_complete_container_names function does something like the following
# COMPREPLY=( $(compgen -W "(list of containers)" -- "$cur") )
# documentaton : http://www.tldp.org/LDP/abs/html/tabexpansion.html
# WHERE : $cur is the current value of the cursor that should be autocompleted
#
# To check the function already available on your terminal, run the command
# `typeset -F` - i'd grep it with what i want
# `typeset -F | grep -i git`
# NOTE : These are all functions available, all of they may not be for autocompletion
# For git: check https://github.com/git/git/blob/master/contrib/completion/git-completion.bash
#
# docker exec sh on the application
#
# complete adds autocompletion
# expects docker available in /etc/bash_completion.d
# https://docs.docker.com/machine/completion/
complete -F __docker_complete_container_names shocker
shocker() {
docker exec -it $1 sh
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment