Skip to content

Instantly share code, notes, and snippets.

@joshbode
Last active April 17, 2016 16:42
Show Gist options
  • Save joshbode/a77edcfe938315e1d57b4701ca99f3af to your computer and use it in GitHub Desktop.
Save joshbode/a77edcfe938315e1d57b4701ca99f3af to your computer and use it in GitHub Desktop.
Interactive ZSH wrapper/alias for versioned Python commands to select version when no version provided (e.g. ipython -> ipython2 or ipython3)
_py_version() {
PY_VERSIONS=(2 3)
LINE=(${(s: :)history[$HISTCMD]})
COMMAND=${LINE[1]}
ARGS=${(j: :)LINE[2,-1]}
while (( 1 )) {
read -s -k1 VERSION"?$COMMAND [${PY_VERSIONS[1]}]: "
if [[ $VERSION == $'\n' ]]; then
VERSION=${PY_VERSIONS[1]}
break
elif (( ${PY_VERSIONS[(I)$VERSION]} )); then
echo $VERSION
break
fi
echo "$VERSION is not supported (${(j:, :)PY_VERSIONS})"
}
if [[ -x $(which $COMMAND$VERSION) ]]; then
COMMAND=$COMMAND$VERSION
else
COMMAND="python$VERSION $(which -a $COMMAND | grep -v aliased | head -n 1)"
fi
eval $COMMAND $ARGS
}
# versioned python commands
alias python="_py_version"
alias pip="_py_version"
alias pydoc="_py_version"
alias idle="_py_version"
alias ipython="_py_version"
alias jupyter="_py_version"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment