Johnny Matthews | Open Neovim at Specified Directory

Right, I get that this is pretty niche, but it’s something I run into all the time. I’m pretty much only posting it here so I remember to do this next time I reformat everything (likely next week)… This was written on 2nd of October 2025.

Ok so here’s the situation, say you’ve got the following tree structure:

~/genau
❯ tree
.
├── example
│   ├── one.txt
│   ├── three.sh
│   └── two.md
├── stuff
└── thing

If you run nvim example, Neovim will open up in the ~/genau directory. But that’s not what I want! I want Neovim to cd into the example directory and open that up as the pwd.

Incredible idea, I know. And it’s an achievable goal! All you’ve gotta do it add a function to your ~/.zshrc file (or .bashrc or .fshrc or whatever shell the kids are using these days.)

Here’s the function:

nvim() {
    if [ $# -eq 1 ] && [ -d "$1" ]; then
        command nvim -c "cd $1"
    else
        command nvim "$@"
    fi
}

You may need to remove any aliases that use nvim – not sure if they’ll cause conflicts but it’s worth noting all the same.

That’s about it.