Source file in zsh when entering directory

Is there a way to configure a specific file to configure the environment when entering a specific directory? Looks like rvm does, but more general.

+4
source share
3 answers

IMHO, you should not use an alias for this, but add any directory change to it:

autoload -U add-zsh-hook load-local-conf() { # check file exists, is regular file and is readable: if [[ -f .source_me && -r .source_me ]]; then source .source_me fi } add-zsh-hook chpwd load-local-conf 

This hook function will work with any directory change.

FWIW, if you want to change directories without using hooks, use cd -q dirname

+14
source

You can define the function that cd will execute, and then the source file. This function will try to set the .source_me source to a new directory if it exists:

 mycd () { builtin cd $@ [ $? -eq 0 -a -f .source_me ] && source .source_me } 

Enable function with

 alias cd=mycd 
+1
source

What you want to achieve is to set environment variables for a specific directory.

Compared to other tools designed for this, direnv is the best of them. One of the main advantages is that it supports the unloading of environment variables when exiting this directory.

direnv is the environment switch for the shell. It can connect to bash, zsh, tcsh, fish shell and elvish to load or unload environment variables depending on the current directory. This allows project - specific environment variables without cluttering the ~/.profile file.

What distinguishes direnv from other similar tools:

  • direnv written in Go, faster than its Python counterpart
  • direnv supports unloading environment variables when exiting a specific directory
  • direnv covers a lot of shells

Similar projects

  • Environment modules - one of the oldest (in a good way) environment loading systems
  • autoenv - light weight; does not support unloading; written slowly in Python
  • zsh-autoenv is a multifunctional mixture of autoenv and smartcd : input / output of events, attachment, copying (only Zsh).
  • asdf - pure bash solution with plugin system
0
source

Source: https://habr.com/ru/post/1485669/


All Articles