Skip to content

Instantly share code, notes, and snippets.

@Mic92
Last active October 8, 2016 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mic92/1cc866df56154027aae9438d19e657e1 to your computer and use it in GitHub Desktop.
Save Mic92/1cc866df56154027aae9438d19e657e1 to your computer and use it in GitHub Desktop.
zshrc clever functions
# cd to files' directories
cd() {
local to="${1:-$HOME}"
if [ -f "$to" ]; then
to="$(dirname $to)"
fi
builtin cd "$to"
}
upfind() {
local previous=
local current=$PWD
if [[ $# -ne 1 ]];then
echo "$0 FILE_NAME"
return 1
fi
while [[ -d $current && $current != $previous ]]; do
local target_path=$current/$1
if [[ -f $target_path ]]; then
echo $target_path
return 0
else
previous=$current
current=$current:h
fi
done
return 1
}
# build project from subdirectory
ninja(){
local build_path="$(dirname "$(upfind "build.ninja")")"
command ninja -C "${build_path:-.}" "$@"
}
make(){
local build_path="$(dirname "$(upfind "Makefile")")"
command make -C "${build_path:-.}" "$@"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment