Today's Daily Double is... A SNIPPET!!!
For quite a while I've been making aliases that I add to my .zshrc shell so that I can just navigate directly to repos from the root of my terminal without having to go through a series of directories and remember where shit is. So for example the Hub repo lives at callum/telleytelley/multiplato/hub, and to get there requires a lotta of cd-ing and generally is a pain in the ass. So I added an alias so that whenever I type and enter 'hub' in iterm2 at the root I directly navigate to the Hub. This was sorta nice so I started adding similar aliases for other repos I was contributing to. Neat, but I found that the maintenance of continually having to update my .zshrc was a bit of a pain in the ass, especially if the projects and thus codebases I work in frequently changed. So I made a more reusable snippet that just takes whatever text you enter at the root of your repo in your terminal, and so long as a repo exists in one of the directories you configured to be included within that search, it will directly navigate to that repo. I've only had this going for a couple hours at this point and I feel sure that one day I'm gonna run into some sort of caveat where I have a repo with a name that is a reserved symbol or something of the sort- but until then, I'll enjoy a few saved keystrokes.
anyway
vim ~/.zshrc (or w/e you prefer if heathen) and add......
create_repo_aliases() {
local base_dir="$1"
[[ -d "$base_dir" ]] || return
for path in "$base_dir"/*; do
[[ -d "$path" ]] || continue
local name="${path##*/}"
local sanitized="${name//[^[:alnum:]]/_}"
sanitized="${sanitized:l}"
sanitized="${sanitized//__/_}"
sanitized="${sanitized##_}"
sanitized="${sanitized%%_}"
[[ -n "$sanitized" ]] || continue
alias "$sanitized"="cd $(printf "%q" "$path")"
if [[ "$name" =~ ^[[:alpha:]_][[:alnum:]_]*$ ]] && [[ "$name" != "$sanitized" ]];
then
alias "$name"="cd $(printf "%q" "$path")"
fi
done
}
create_repo_aliases "$HOME/bronson"
create_repo_aliases "$HOME/tellytelly"
(add the directories you want to include via create_repo_aliases - for me, bronson is where personal projects live and tellytelly is where work projects live)