Skip to content

Instantly share code, notes, and snippets.

@pjaspers
Created April 16, 2010 13:14
Show Gist options
  • Save pjaspers/368394 to your computer and use it in GitHub Desktop.
Save pjaspers/368394 to your computer and use it in GitHub Desktop.
Autocomplete zsh projects folder
function go() {
if [[ -e ~/development/rails/$1 ]]; then
cd ~/development/rails/$1
elif [[ -e ~/development/sinatra/$1 ]]; then
cd ~/development/sinatra/$1
elif [[ -e ~/development/objc/$1 ]]; then
cd ~/development/objc/$1
elif [[ -e ~/development/php/$1 ]]; then
cd ~/development/php/$1
elif [[ -e ~/development/ruby/$1 ]]; then
cd ~/development/ruby/$1
else
cd
fi
}
_go_list_need_generating () {
if [ ! -f ~/.projectlist~ ]; then return 0;
else
return 1;
fi
}
_go () {
if _go_list_need_generating; then
echo "\nGenerating ~/.projectlist~..." > /dev/stderr
for f in ~/development/*; do ls $f | cat >> ~/.projectlist~; done
fi
compadd `cat ~/.projectlist~`
}
compdef _go go
@DefV
Copy link

DefV commented Apr 20, 2010

My implementation of the same code:

PROJECT_PATHS=(~/Projects/Openminds ~/Projects/RubyRails ~/Projects/CRSolutions)

function project () {
cmd="cd"
file=$1

if [[ "open" == "$file" ]] then
    file=$2
    cmd=(${(s: :)EDITOR})
fi

for project in $PROJECT_PATHS; do
    if [[ -d $project/$file ]] then
        $cmd "$project/$file"
        unset project # Unset project var
        return
    fi
done

echo "No such project $1"
}  

alias p="project"
alias m="project open "

function _project () {
    compadd `/bin/ls -l $PROJECT_PATHS | awk '{ print $9 }'`
} 

compdef _project project

@pjaspers
Copy link
Author

Awesome. Thx !

@To1ne
Copy link

To1ne commented Feb 5, 2013

I've turned this into an oh-my-zsh plugin and requested a pull:
ohmyzsh/ohmyzsh#1583

I've renamed it to pj to avoid a name clash with the GO programming language.

@johnjohndoe
Copy link

@To1ne Please add a README to the folder of oh-my-zsh.

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