Last active
September 16, 2023 15:50
-
-
Save blumonkey/16f48a0598162b85b60e6df999dd49da to your computer and use it in GitHub Desktop.
vac: A simple bash command that activates virtualenv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This setup defines a new command `vac` that activates a virtualenv. You can also add autocompletion to this command. | |
# Prereq: All venv directories must be in the same place, I place mine under `~/venvs` | |
# Function to activate virtualenv. Place this in your `.bashrc` file. | |
``` | |
vac() { | |
# activates a virutalenv environment | |
# eg: vactivate llama_farm | |
source "$HOME/venvs/$1/bin/activate" | |
} | |
``` | |
# If you want autocompletion with the `vac` command, add the following to a file | |
# `~/.local/share/bash-completion/completions/vac-completion` | |
# auto completions for the `vac` command. | |
``` | |
_vac_completions() | |
{ | |
local cur prev envs | |
COMPREPLY=() | |
cur="${COMP_WORDS[COMP_CWORD]}" | |
prev="${COMP_WORDS[COMP_CWORD-1]}" | |
# list all envs, I place mine in a folder ~/venvs | |
envs=$(ls $HOME/venvs) | |
COMPREPLY=( $(compgen -W "${envs}" -- ${cur}) ) | |
return 0 | |
} | |
complete -F _vac_completions vac | |
``` | |
# Then add this file to your `.bashrc` too: | |
`source ~/.local/share/bash-completion/completions/vac-completion` | |
# Now your setup is complete! | |
# Ref: https://askubuntu.com/a/345150 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment