Created
June 12, 2014 14:22
-
-
Save ttscoff/c96b6c454be6937e5c7e to your computer and use it in GitHub Desktop.
cdt function to change directory based on a tag filing system
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In my tag-filing system where top-level "context" folders are tagged with | |
# "=Context" tags and subfolders are tagged with "@project" tags, this function | |
# lets me quickly cd to any tagged folder in my filing system. The first | |
# argument is a context folder, the rest are a chain of subfolders. The | |
# subfolders don't need to be contiguous, and the matching is fuzzy. | |
cdt() { | |
local fuzzy | |
local root | |
local sub | |
local list | |
if [[ $# == 0 || $1 =~ ^\-?\-h(elp)?$ ]]; then | |
cat <<-EOS | |
$FUNCNAME changes directory based on tagged folders | |
usage: $FUNCNAME context [subfolder, [subfolder...]] | |
EOS | |
return | |
fi | |
fuzzy=`echo ${1##[#=]} | sed 's/\([[:alpha:]]\)/\1*/g'` | |
root=`mdfind "kMDItemUserTags = '=*$fuzzy'c && kMDItemContentType = 'public.folder'"|head -n 1` | |
if [[ $2 ]]; then | |
shift | |
while [[ $1 ]]; do | |
fuzzy=`echo $1 | tr -d "@: " | sed 's/\([[:alpha:]]\)/\1*/g'` | |
sub=`mdfind -onlyin "$root" "kMDItemUserTags = '@$fuzzy'c && kMDItemContentType = 'public.folder'"|head -n 1` | |
list=`ls -1F | grep -i "${fuzzy//\*/.*}.*\/$" | head -n 1` | |
if [[ -n $sub ]]; then | |
root=$sub | |
elif [[ -d $root/$list ]]; then | |
root=$list | |
fi | |
shift | |
done | |
fi | |
builtin cd "$root" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment