Skip to content

Instantly share code, notes, and snippets.

@nicolasramy
Created January 30, 2014 00:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicolasramy/8700101 to your computer and use it in GitHub Desktop.
Save nicolasramy/8700101 to your computer and use it in GitHub Desktop.
Simple bash_completion file: pip, venv-* functions. (venv-functions details: https://gist.github.com/nicolasramy/6438236#file-bash_functions_python)
#!/bin/sh
_pip_completion() {
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
first="${COMP_WORDS[0]}"
commands=$($first --help | awk '/Commands\:/,/General Options\:/' | \
\grep -E -o "^\s{2}\w*" | tr -d ' ')
opts=$($first --help | \grep -E -o "((-\w{1}|--(\w|-)*=?)){1,2}")
if [ $COMP_CWORD == 1 ] ; then
COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) )
return 0
fi
if [[ ${cur} == -* ]] ; then
local command_opts=$($first $prev --help | \
\grep -E -o "((-\w{1}|--(\w|-)*=?)){1,2}")
COMPREPLY=( $(compgen -W "${command_opts}" -- ${cur}) )
return 0
fi
}
complete -o default -F _pip_completion pip
complete -o default -F _pip_completion pip2
complete -o default -F _pip_completion pip3
#!/bin/sh
_list_virtual_envs() {
local choix selection
case "$COMP_CWORD" in
# On 1st parameter, list '~/.virtualenvs' folder
1)
available_envs=$( ls ~/.virtualenvs/ )
;;
*)
words=()
;;
esac
# Create list choice
selection=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( $( compgen -W '$available_envs' -- $selection ) )
}
complete -F _list_virtual_envs venv-activate
complete -F _list_virtual_envs venv-remove
@nicolasramy
Copy link
Author

You have to make it available in /etc/bash_completion.d/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment