June 26, 2020

How to automatically change the current working directory in Neovim

Something that has been overly annoying ever since I switched over to Neovim from Vim is the fact that my working directory has alway been my home folder, no matter what file I’m editing.

I first though it was an issue with the plugin Ranger.vim and I didn’t do anything about it. I exclusively use that plugin to navigate my filesystem and open files in Neovim using Ranger (and previously in Vim) and I can’t live without it.

It wasn’t until I recently that I noticed that I had the same issue when using the built in command :edit that I did some research about it and found out about the feature autochdir:

'autochdir' 'acd'       boolean (default off)
        When on, Vim will change the current working directory whenever you
        open a file, switch buffers, delete a buffer or open/close a window.
        It will change to the directory containing the file which was opened
        or selected.

When enabled it means that when I’m working on a file in the folder $HOME/example/ and then open a new buffer and spawn Ranger inside Neovim (or just using :edit) I end up in the folder $HOME/example and no in my home folder $HOME/.