agkozak/zsh-z

z into a folder with same name as current folder

Closed this issue · 3 comments

Hey,
Whats the best way to z into a folder with same name as current folder?
Ex: when Im in /mnt/hostname, and Im trying to go into /mnt/hostname/hostname,
trying to z into the subdir with the same name as the current dir isnt working. z hostname keeps me in /mnt/hostname/ but Im trying to change dir into /mnt/hostname/hostname. Im not sure why it supercedes the subdir with actually staying in the current dir

Whats the best way?

If you type

z -l hostname

I think you'll find that right now /mnt/hostname outranks /mnt/hostname/hostname -- you've been spending more time in the former, or you've been there very recently. If one possibility outscores another, there's nothing you can do about that.

What you can do is to type

z host<TAB>

and I bet you'll find /mnt/hostname/hostname in the list that pops up. Tab over to it and hit return.

For that matter, you might find typing cd host<TAB> to be the easiest way to get there. Zsh-z isn't meant to replace cd; it's a faster alternative most of the time, though.

Thanks, 100% correct on all points.
I had aliased cd to z. Is there a better approach, or just muscle memorize z instead of cd? @agkozak

My rule is never to use aliases for anything as fundamental as cd -- too much can go wrong. The two commands don't really do the same thing -- they both allow you to change directories, yes, but cd takes a precise, unambiguous argument, whereas z takes a short string and tries to guess what you want. By adopting the habit of using each command when it's called for, you can reach a point where you're doing minimal typing and getting where you want to go nearly all the time.

Another feature of Zsh that I couldn't recommend more highly is static named directories. My last name is Kozak, and it usually shows up in my user name on each system I use, so z z could take me to just about anything I work on. But in my .zshrc I have

[[ -d ${HOME}/.zcomet/repos/agkozak/zsh-z ]] && hash -d z="${HOME}/.zcomet/repos/agkozak/zsh-z"

The hash command allows me to create a directory ~z pointing to wherever the code for Zsh-z is kept. I can type cd ~z to get there, or, with the CDABLE_VARS option on, just cd z.