Skip to content

Instantly share code, notes, and snippets.

@sasagon
Created June 14, 2012 05:23
Show Gist options
  • Save sasagon/2928151 to your computer and use it in GitHub Desktop.
Save sasagon/2928151 to your computer and use it in GitHub Desktop.
ディレクトリに出入りするとき、そこにあるスクリプトファイルを自動的にsourceで取り込む(.zshrc用)
# read .swenv.(out|in) when changing the current directory
function swenv_out() {
predir=~1
outfile="${predir}/.swenv.out"
[ -d "$predir" -a -e "$outfile" -a -O "$outfile" -a -w "$outfile" ] && source $outfile
}
function swenv_in() {
infile=".swenv.in"
[ -e $infile -a -O $infile -a -w $infile ] && source $infile
}
function swenv_chpwd() {
swenv_out
swenv_in
}
setopt auto_pushd
typeset -ga chpwd_functions
chpwd_functions+=swenv_chpwd
swenv_in
@sasagon
Copy link
Author

sasagon commented Jun 14, 2012

Notes

Paste the code snippet in your .zshrc, and you can switch the shell settings automatically when you changing the current directory.

You can write the local (the directory only) settings in .swenv.in. If the file exists, your zsh reads it with source command when entering the directory.

JAVA_HOME=$HOME/lib/jdk/jdk1.6.0_26
PATH="$JAVA_HOME/bin:$PATH"

And you also can write global settings in .swenv.out.

JAVA_HOME=$HOME/lib/jdk/jdk1.7.0_03
PATH="$JAVA_HOME/bin:$PATH"

Your zsh will read it when leaving the directory.

If neither .swenv.in nor .swenv.out exists in the entering/leaving directories, your zsh does nothing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment