When comes into the root folder of a project, it's often necessary to setup environment variables, define some aliases, etc.
You can put all these things into a shell script,
and source
it manually.
And put the clean up commands into another script,
run it when you leave the folder.
However these are tidious and easy to be forgetted. If you forget to run these scripts, things may be messed up.
The best solution are run these scripts automatically when entering and leaving a folder.
The following tools are for this purpose.
direnv
direnv is written in Go and more widely used. Compared with autoenv, it cleanup environment automatically. So you only need define one file: .envrc.
Unfortunately it doesn't support definition of alias. By itself this is not a big problem. But when used with vimrcs, it will mess configurations of different applications up.
For example, to define the following alias:
alias vi="XDG_CONFIG_HOME=$HOME/Documents/sources/vimrcs/asciidoc nvim"
you have to write it as
export XDG_CONFIG_HOME=$HOME/Documents/sources/vimrcs/asciidoc`
in the .envrc file. However, direnv, with many other applications, uses XDG_CONFIG_HOME, too. So direnv will write rule files into folder ~/Documents/sources/vimrcs/asciidoc/direnv, instead of ~/.config/direnv.
As a workaround, you have to add the following codes into ~/.zshrc:
function vi() {
case $VIMODE in
asciidoc) XDG_CONFIG_HOME=$HOME/Documents/sources/vimrcs/asciidoc nvim $@ ;;
python) XDG_CONFIG_HOME=$HOME/Documents/sources/vimrcs/python nvim $@ ;;
*) XDG_CONFIG_HOME=$HOME/Documents/sources/vimrcs/text nvim $@ ;;
esac
}
Then add export VIMODE=asciidoc
in .envrc to make vi take the specified folder
as its configuration home.
autoenv
autoenv is written in pure shell. No need to compile. Download it to the right place and add it to startup script:
git clone git://github.com/inishchith/autoenv.git ~/.autoenv
echo 'source ~/.autoenv/activate.sh' >> ~/.zshrc
To enable auto cleanup when leaving a folder, add the following line into ~/.autoenv/activate.sh:
AUTOENV_ENABLE_LEAVE="abc"
Note:
-
Do NOT install it with
pip
. It doesn't work for zsh. -
If you can't clone it due to the network issue, it's OK to download the script directly:
wget https://raw.githubusercontent.com/inishchith/autoenv/master/activate.sh -O ~/.autoenv/activate.sh
Make it Compatible with Folder Jumping Tools
To work with fuzzy folder jumper z,
replace builtin cd
(two occurances) with autoenv_cd
in
~/.oh-my-zsh/plugins/z/z.sh.
See issue 154 for details.
It's said fasd (an alternative of z) works well
out-of-box with autoenv. However, fasd introduces d
command,
which is conflict with an existing command.
So I decided to use z
instead of fasd.
Usage
Add setup and cleanup commands into .env and .env.leave respetively.
For example:
cat .env
. env/bin/activate
alias vi="XDG_CONFIG_HOME=$HOME/Documents/sources/vimrcs/asciidoc nvim"
cat .env.leave
deactivate
alias vi=nvim
Problem
autoenv have no concept of project tree.
When you cd
to a subfolder inside a project,
it will run unloading and loading scripts just as you jump to
an outside folder.
Thus to keep the environment, you have to run all commands (including vi) at the root folder of the project, which is very unconvenient.
See autoenv executes even in subfolder for details.
Summary
When the project has a flat structure, you always run commands in the root folder, autoenv is an option. Otherwise, by far direnv is the only option, together with the function definitions in ~/.zshrc.