Skip to content

Instantly share code, notes, and snippets.

@nshafer
Last active July 14, 2022 16:27
Show Gist options
  • Save nshafer/330ca860b0f1180a2990d5afd5f70234 to your computer and use it in GitHub Desktop.
Save nshafer/330ca860b0f1180a2990d5afd5f70234 to your computer and use it in GitHub Desktop.
Extend the virtualenvwrapper's `workon` command with other non-python project directories
#!/bin/bash
# Extend the `workon` command to include just random directories to CD into without activating
# a virtualenv.
#
# Source into .bashrc or .bash_profile
#
# source ~/custom_workon.sh
declare -A projects
# Add projects here, as many as you want
projects[project1]="/path/to/project1"
projects[project2]="/path/to/project2"
# Include the original virtualenvwrapper script
source /usr/share/virtualenvwrapper/virtualenvwrapper.sh
save_function() {
local ORIG_FUNC=$(declare -f $1)
local NEWNAME_FUNC="$2${ORIG_FUNC#$1}"
eval "$NEWNAME_FUNC"
}
# save virtualenvwrapper's workon function
save_function workon vew_workon
workon() {
if [[ -n ${projects[$1]} ]]; then
cd ${projects[$1]}
else
# Fallback to virtualenvwrapper
vew_workon $@
fi
}
# Autocomplete
_projects_virtualenvs () {
local cur="${COMP_WORDS[COMP_CWORD]}"
local words="`virtualenvwrapper_show_workon_options` ${!projects[@]}"
COMPREPLY=( $(compgen -W "${words}" -- ${cur}) )
}
# override the complete call for the `workon` command
complete -o default -o nospace -F _projects_virtualenvs workon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment