If you have all your code repos under a common directory, you can make it so changing your working directory to any of the repos is just entering the name of the repo.
For example, all my code repos are under ~/code
:
~ > ls ~/code
bar foo
By adding a loop for the sub-directories of the common parent inside your shell config, ~/.zshrc
in my case, you can automatically create a alias for each of the repo directories:
for repo in $(ls ~/code)
do
alias $repo="cd ~/code/$repo"
done
Then you need to reload your config:
~ > . ~/.zshrc
Now, if you're in one repo, lets say foo
:
~/code/foo/app/models >
You can easily swap over to bar:
~/code/foo/app/models > bar
~/code/bar >
Top comments (9)
Hey nc idea, expanded it a little so you can provide more locations
A comment on a different site has given, for zsh, what I believe is the best solution for this:
Set
cdpath=($HOME/code)
in.zshrc
. Withauto_cd
this allows for the same functionality without creating a bunch of aliases.What's the thing with โ
$(ls ~/code)
โ? Why spawn a subshell, rather than just saying โfor repo in ~/code/*
โ?now you just have to be cautious to not check out any project having a name like a command on your machine. (like
git clone https://server/myproject.git ~/code/ls
)Yeah, that's clearly a risk, but most likely a outlier.
You could always add constraint in the loop to skip the iteration if the command already exists, as well.
Try autojump ;)
What seems to be a good solution is using
cdpath
in combination withauto_cd
in zsh, should produce the same results without the aliasing.ZSH's plugin Z does almost the same job. It requires you to use the command z in front of the folder name but it does not pollute your shell with aliases e.g. z foo OR z bar
@nickelkr it very handy. I've a slightly better way here