Vim has an "autochdir" mode whereby the current directory of each buffer is the working directory of the file.
It sucks. For instance, when you're editing a .git/COMMIT_EDITMSG, it makes the directory .git/. It has no concept of intelligent exceptions.
I made myself a better one:
On startup, we save the current (i.e. project top) directory in the `$top` variable:
:let $top=getcwd()
When entering new buffer, we lcd to the directory of the path: :au BufWinEnter * exec "lcd " . expand('%:h')
But if the buffer is new, without a name, we change to the top. :au BufWinEnter * if expand("%") == "" | exec "lcd $top" | endif
If we are in .git, go to top. :au BufWinEnter *.git/* exec "lcd $top"
Why is the variable $top and not top? The dollar sign makes it an environment variable, exported to child processes. Why on earth would we do that?Because Vim interpolates $-sigiled environment variables in more contexts than regular variables! I can use $top pretty much anywhere.