1
0
mirror of https://github.com/Tarrasch/zsh-autoenv.git synced 2024-11-25 00:10:59 +02:00
Go to file
Daniel Hahler bcfdd75382 _autoenv_source: grep for unstash also
Just for consistency.
2017-08-19 15:39:59 +02:00
lib Fix use of local in while loop. (#66) 2017-05-16 22:58:25 +02:00
tests Minor cleanup 2017-08-18 15:27:32 +02:00
.gitignore gitignore tests/*.err 2014-12-02 22:49:01 +01:00
.travis.yml travis: fix tests 2015-04-23 21:52:25 +02:00
autoenv.plugin.zsh travis: fix tests 2015-04-23 21:52:25 +02:00
autoenv.zsh _autoenv_source: grep for unstash also 2017-08-19 15:39:59 +02:00
init.zsh Create init.zsh to make compatible with prezto 2015-01-19 15:25:42 +01:00
Makefile Makefile: allow to pass in TEST_SHELL (#60) 2017-04-23 14:26:22 +02:00
README.md Add autoenv-edit function to edit current env files (#68) 2017-08-11 14:15:46 +02:00

Bitdeli Badge

Build Status

Autoenv for Zsh

zsh-autoenv automatically sources (known/whitelisted) .autoenv.zsh files, typically used in project root directories.

It handles "enter" and leave" events, nesting, and stashing of variables (overwriting and restoring).

Features

  • Support for enter and leave events, which can use the same file. By default .autoenv.zsh is used for entering, and .autoenv_leave.zsh for leaving.
  • Interactively asks for confirmation / authentication before sourcing an unknown .autoenv.zsh file, and remembers whitelisted files by their hashed content.
  • Test suite.
  • Written in Zsh.

Variable stashing

You can use autostash in your .autoenv.zsh files to overwrite some variable, e.g. $PATH. When leaving the directory, it will be automatically restored.

% echo 'echo ENTERED; autostash FOO=changed' > project/.autoenv.zsh
% FOO=orig
% cd project
Attempting to load unauthorized env file!
-rw-rw-r-- 1 user user 36 Mai  6 20:38 /tmp/project/.autoenv.zsh

**********************************************

echo ENTERED; autostash FOO=changed

**********************************************

Would you like to authorize it? (type 'yes') yes
ENTERED
project % echo $FOO
changed
% cd ..
% echo $FOO
orig

There is also stash, unstash and autounstash, in case you want to have more control.

The varstash library has been taken from smartcd, and was optimized for Zsh.

Writing your .autoenv.zsh file

autoenv_source_parent()

zsh-autoenv will stop looking for .autoenv.zsh files upwards after the first one has been found, but you can use the function autoenv_source_parent to source the next .autoenv.zsh file upwards the directory tree from there.

The function accepts an optional argument, which allows to stop looking before the file system root is reached:

autoenv_source_parent ../..

Installation

Clone the repository and source it from your ~/.zshrc file:

% git clone https://github.com/Tarrasch/zsh-autoenv ~/.dotfiles/lib/zsh-autoenv
% echo 'source ~/.dotfiles/lib/zsh-autoenv/autoenv.zsh' >> ~/.zshrc

Using antigen

antigen-bundle Tarrasch/zsh-autoenv

Using zgen

Add the following to your .zshrc where you are loading your plugins:

zgen load Tarrasch/zsh-autoenv

Using zplug

Add the following to your .zshrc where you are loading your plugins:

zplug "Tarrasch/zsh-autoenv"

Configuration

You can use the following variables to control zsh-autoenv's behavior. Add them to your ~/.zshrc file, before sourcing/loading zsh-autoenv.

AUTOENV_FILE_ENTER

Name of the file to look for when entering directories.

Default: .autoenv.zsh

AUTOENV_FILE_LEAVE

Name of the file to look for when leaving directories. Requires AUTOENV_HANDLE_LEAVE=1.

Default: .autoenv_leave.zsh

AUTOENV_LOOK_UPWARDS

Look for zsh-autoenv "enter" files in parent dirs?

Default: 1

AUTOENV_HANDLE_LEAVE

Handle leave events when changing away from a subtree, where an "enter" event was handled?

Default: 1

AUTOENV_DISABLED

(Temporarily) disable zsh-autoenv. This gets looked at in the chpwd handler.

Default: 0

AUTOENV_DEBUG

Enable debugging. Multiple levels are supported (max 2).

  • 0: no debug messages
  • 1: generic debug logging
  • 2: more verbose messages
    • messages about adding/removing files on the internal stack
  • 3: everything
    • sets xtrace option (set -x) while sourcing env files

Default: 0

Usage

zsh-autoenv works automatically once installed.

You can use autoenv-edit to edit the nearest/current autoenv files. It will use $AUTOENV_EDITOR, $EDITOR, or vim for editing.

Recipes

Automatically activate Python virtualenvs

Given AUTOENV_FILE_ENTER=.autoenv.zsh, AUTOENV_FILE_LEAVE=.autoenv.zsh and AUTOENV_HANDLE_LEAVE=1 the following script will activate Python virtualenvs automatically in all subdirectories (.venv directories get used by pipenv with PIPENV_VENV_IN_PROJECT=1):

# Environment file for all projects.
#  - (de)activates Python virtualenvs (.venv) from pipenv

if [[ $autoenv_event == 'enter' ]]; then
  autoenv_source_parent

  _my_autoenv_venv_chpwd() {
    if [[ -z "$_ZSH_ACTIVATED_VIRTUALENV" && -n "$VIRTUAL_ENV" ]]; then
      return
    fi

    setopt localoptions extendedglob
    local -a venv
    venv=(./(../)#.venv(NY1:A))

    if [[ -n "$_ZSH_ACTIVATED_VIRTUALENV" && -n "$VIRTUAL_ENV" ]]; then
      if ! (( $#venv )) || [[ "$_ZSH_ACTIVATED_VIRTUALENV" != "$venv[1]" ]]; then
        unset _ZSH_ACTIVATED_VIRTUALENV
        echo "De-activating virtualenv: ${(D)VIRTUAL_ENV}" >&2
        deactivate
      fi
    fi

    if [[ -z "$VIRTUAL_ENV" ]]; then
      if (( $#venv )); then
        echo "Activating virtualenv: ${(D)venv}" >&2
        source $venv[1]/bin/activate
        _ZSH_ACTIVATED_VIRTUALENV="$venv[1]"
      fi
    fi
  }
  autoload -U add-zsh-hook
  add-zsh-hook chpwd _my_autoenv_venv_chpwd
  _my_autoenv_venv_chpwd
else
  add-zsh-hook -d chpwd _my_autoenv_venv_chpwd
fi

History

This started as an optimized version of the bash plugin autoenv but for Zsh, and grew a lot of functionality on top of it (inspired by smartcd).

The code was initially based on @joshuaclayton's dotfiles. In September 2013 @Tarrasch packaged it into a nice antigen-compatible unit with integration tests. Since November 2014, @blueyed took over and added many many nice features, mainly inspired by smartcd.