shell scripts for navigating to and activating an associated virtual environment (MacOS)
Scripts in this project enable a new shell command for MacOS Terminal, goto
, which changes directory to a specific project directory then activates the associated virtual environment. If a non-existent virtual environment name is supplied, no virtual enviroment will be activated. However, the directory will still be changed to the location specified in a system variable called $GOTO_HOME
. This handy shortcut ($ goto example
) takes the place of opening a new Terminal window and executing workon example
followed by $ cd /your/file/directory/
.
This assumes use of virtualenvwrapper
on MacOS High Sierra. Requirements include virtualenv
and virtualenvwrapper
.
virtualenvwrapper
The following line creates a virtual environment using Python3 called "example":
$ mkvirtualenv --python=python3 example
.bash_profile
file if it doesn't exist.$ touch ~/.bash_profile
GOTO_HOME
system variable in your .bash_profile
file (typically ~/.bash_profile
)The GOTO_HOME
system variable should be the base path for where you store your projects, or a directory for goto
to default to if no valid environment is supplied.
$ touch ~/.bash_profile
$ echo -e "\nexport GOTO_HOME=/your/file/directory/">>~/.bash_profile
or open the .bash_profile
in the default text editor with $ open ~/.bash_profile
and type export GOTO_HOME=/your/file/directory/
into the file directly.
Example .bash_profile
File:
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/code
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages'
export RESOPS_HOME=$HOME/Desktop/mbk/resops_results
export GOTO_HOME=$HOME/Desktop/mbk
source /usr/local/bin/virtualenvwrapper.sh
GOTO_HOME
to the folder where I store all of my active local git repositories.goto
ScriptThe content of the goto
script must be updated for your Python virtual environments, created using virtualenvwrapper
commands mkvirtualenv yourenv
. For each project you'd like to include, insert an elif
statement defining the project directory DESTINATION
based on the specified virtual environment.
Example:
elif [ "$1" == "resops" ]; then
DESTINATION="${GOTO_HOME}/resops/folsom"
goto
and cleanto
scripts to /usr/local/bin/
Modify the file permissions for each script to allow use as an executable
$ cd /usr/local/bin/
$ chmod 711 goto
$ chmod 711 cleanto
workon $WINDOWENV; cleanto; clear
to Run Commandgoto
script in the new Terminal window with virtualenvwrapper's workon
, then cleans up the .bash_profile
with cleanto
.Install the Sublime Text CLI documented at https://www.sublimetext.com/docs/3/osx_command_line.html to add support for automatically opening a Sublime Text window in the goto
destination directory.
Open the Terminal and run to create a symlink subl
for the Sublime Text CLI.
$ ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" /usr/local/bin/subl
Update the Bash Profile PATH variable by adding the following to .bash_profile
:
export PATH=$HOME/bin:$PATH
Add the following to the end of goto
:
if [ "$2" == "-s" ]; then
subl -n -a $DESTINATION
fi
This opens a new Sublime window in the target directory when goto
is invoked from the command line with the argument -s
.
Configuring the goto
script enables you to change directories to the project directory most commonly used with a given virtual environment. This handy shortcut takes the place of workon example
followed by $ cd /your/file/directory/
.
Open Terminal with the updated profile and run:
$ goto example -s
Result:
Changes directory to the project directory configured in goto
then activates the example
virtual environment. If the optional -s
flag is included, a new Sublime window will be opened in the project directory. If a non-existent virtual environment name is supplied, no virtual enviroment will be activated. However, the directory will still be changed to the location specified in $GOTO_HOME
.
You may need to update the permissions on /usr/bin/local/ in OS 14 with
$ sudo chown -R $(whoami) /usr/local/bin