1 [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/Tarrasch/zsh-autoenv/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
3 [![Build Status](https://travis-ci.org/Tarrasch/zsh-autoenv.svg?branch=master)](https://travis-ci.org/Tarrasch/zsh-autoenv)
7 zsh-autoenv automatically sources (known/whitelisted) `.autoenv.zsh` files,
8 typically used in project root directories.
10 It handles "enter" and leave" events, nesting, and stashing of
11 variables (overwriting and restoring).
15 - Support for enter and leave events, which can use the same file.
16 By default `.autoenv.zsh` is used for entering, and `.autoenv_leave.zsh`
18 - Interactively asks for confirmation / authentication before sourcing an
19 unknown `.autoenv.zsh` file, and remembers whitelisted files by their
26 You can use `autostash` in your `.autoenv.zsh` files to overwrite some
27 variable, e.g. `$PATH`. When leaving the directory, it will be automatically
30 % echo 'echo ENTERED; autostash FOO=changed' > project/.autoenv.zsh
33 Attempting to load unauthorized env file!
34 -rw-rw-r-- 1 user user 36 Mai 6 20:38 /tmp/project/.autoenv.zsh
36 **********************************************
38 echo ENTERED; autostash FOO=changed
40 **********************************************
42 Would you like to authorize it? (type 'yes') yes
50 There is also `stash`, `unstash` and `autounstash`, in case you want to
53 The varstash library has been taken from smartcd, and was optimized for Zsh.
56 ## Writing your .autoenv.zsh file
58 ### `autoenv_source_parent()`
60 zsh-autoenv will stop looking for `.autoenv.zsh` files upwards after the first
61 one has been found, but you can use the function `autoenv_source_parent` to
62 source the next `.autoenv.zsh` file upwards the directory tree from there.
64 The function accepts an optional argument, which allows to stop looking before
65 the file system root is reached:
68 autoenv_source_parent ../..
73 Clone the repository and source it from your `~/.zshrc` file:
75 % git clone https://github.com/Tarrasch/zsh-autoenv ~/.dotfiles/lib/zsh-autoenv
76 % echo 'source ~/.dotfiles/lib/zsh-autoenv/autoenv.zsh' >> ~/.zshrc
78 ### Using [antigen](https://github.com/zsh-users/antigen)
80 antigen-bundle Tarrasch/zsh-autoenv
82 ### Using [zgen](https://github.com/tarjoilija/zgen)
84 Add the following to your `.zshrc` where you are loading your plugins:
86 zgen load Tarrasch/zsh-autoenv
88 ### Using [zplug](https://github.com/zplug/zplug)
90 Add the following to your `.zshrc` where you are loading your plugins:
92 zplug "Tarrasch/zsh-autoenv"
96 You can use the following variables to control zsh-autoenv's behavior.
97 Add them to your `~/.zshrc` file, before sourcing/loading zsh-autoenv.
99 ### AUTOENV\_FILE\_ENTER
100 Name of the file to look for when entering directories.
102 Default: `.autoenv.zsh`
104 ### AUTOENV\_FILE\_LEAVE
105 Name of the file to look for when leaving directories.
106 Requires `AUTOENV_HANDLE_LEAVE=1`.
108 Default: `.autoenv_leave.zsh`
110 ### AUTOENV\_LOOK\_UPWARDS
111 Look for zsh-autoenv "enter" files in parent dirs?
115 ### AUTOENV\_HANDLE\_LEAVE
116 Handle leave events when changing away from a subtree, where an "enter"
121 ### AUTOENV\_DISABLED
122 (Temporarily) disable zsh-autoenv. This gets looked at in the chpwd handler.
127 Enable debugging. Multiple levels are supported (max 2).
129 - 0: no debug messages
130 - 1: generic debug logging
131 - 2: more verbose messages
132 - messages about adding/removing files on the internal stack
134 - sets xtrace option (`set -x`) while sourcing env files
140 ### Automatically activate Python virtualenvs
142 Given `AUTOENV_FILE_ENTER=.autoenv.zsh`, `AUTOENV_FILE_LEAVE=.autoenv.zsh` and
143 `AUTOENV_HANDLE_LEAVE=1` the following script will activate Python virtualenvs
144 automatically in all subdirectories (`.venv` directories get used by
145 [pipenv](https://github.com/kennethreitz/pipenv) with
146 `PIPENV_VENV_IN_PROJECT=1`):
149 # Environment file for all projects.
150 # - (de)activates Python virtualenvs (.venv) from pipenv
152 if [[ $autoenv_event == 'enter' ]]; then
153 autoenv_source_parent
155 _my_autoenv_venv_chpwd() {
156 if [[ -z "$_ZSH_ACTIVATED_VIRTUALENV" && -n "$VIRTUAL_ENV" ]]; then
160 setopt localoptions extendedglob
162 venv=(./(../)#.venv(NY1:A))
164 if [[ -n "$_ZSH_ACTIVATED_VIRTUALENV" && -n "$VIRTUAL_ENV" ]]; then
165 if ! (( $#venv )) || [[ "$_ZSH_ACTIVATED_VIRTUALENV" != "$venv[1]" ]]; then
166 unset _ZSH_ACTIVATED_VIRTUALENV
167 echo "De-activating virtualenv: ${(D)VIRTUAL_ENV}" >&2
172 if [[ -z "$VIRTUAL_ENV" ]]; then
173 if (( $#venv )); then
174 echo "Activating virtualenv: ${(D)venv}" >&2
175 source $venv[1]/bin/activate
176 _ZSH_ACTIVATED_VIRTUALENV="$venv[1]"
180 autoload -U add-zsh-hook
181 add-zsh-hook chpwd _my_autoenv_venv_chpwd
182 _my_autoenv_venv_chpwd
184 add-zsh-hook -d chpwd _my_autoenv_venv_chpwd
189 - https://github.com/direnv/direnv
190 - https://github.com/cxreg/smartcd
191 - https://github.com/kennethreitz/autoenv
196 This started as an optimized version of the bash plugin
197 [autoenv](https://github.com/kennethreitz/autoenv) but for Zsh, and grew a lot
198 of functionality on top of it (inspired by [smartcd]).
200 The code was initially based on
201 [@joshuaclayton](https://github.com/joshuaclayton)'s dotfiles.
202 In September 2013 [@Tarrasch](https://github.com/Tarrasch) packaged it into a
203 nice [antigen]-compatible unit with integration tests. Since November 2014,
204 [@blueyed](https://github.com/blueyed) took over and added many many nice
205 features, mainly inspired by [smartcd].
207 [antigen]: https://github.com/Tarrasch/antigen-hs
208 [smartcd]: https://github.com/cxreg/smartcd