shellunixcommand-linemkdircd

mkdir and zoxide into new directory


I use zoxide for directory navigation (i.e. as a replacement for the cd command) on macos. I wanted to have a simple bash function to make a new directory and then immediately zoxide into it.

I made a minor amendment to the nice answer here, as follows:

mcd ()
{
    mkdir -p -- "$1" &&
       z  -P -- "$1"
}

Then I tried to run it in ~/Desktop to create a completely new (i.e., non-existent) directory ~/Desktop/my_templates as follows:

~/Desktop             
> mcd mytemplates
zoxide: no match found

The issue I believe is that zoxide doesn't realize that the new directory mytemplates exists yet, and thus can't navigate to it.

I also tried it by removing the && to see if running mkdir and z as separate commands would help, but the same issue occurred.

Could anyone please explain how to resolve this issue?


Solution

  • It appears that the issue was in the line

    z -P -- "$1"
    

    which I changed to

    z "$1"
    

    Since the -P was a cd specific syntax to resolve symlinks. For zoxide, it appears that to resolve symlinks, this is done via changing the _ZO_RESOLVE_SYMLINKS config setting per here.

    So the final form of the required function is now

    mcd (){
        mkdir -p -- "$1" &&
        z "$1"
    }
    

    which works as expected.