5

How can I (automatically, basically I shouldn't have anything to do apart from initial configuration) set vim cwd to the project root directory (if there's any) or to the current buffer directory (in an autochdir fashion)?

Here is an example of a file path to expected cwd: ~/foo/bar/SOME_FILE -> ~/foo if there is a ~/foo/.git/ dir or a ~/foo/.hg/ dir otherwise ~/foo/bar/

I'm aware of airblade/vim-rooter but it doesn't quite work (e.g. it relies on specific file extensions, and I don't want this behavior).

foo
  • 940
  • 2
  • 9
  • 20
  • `set autochdir` will bring you to current buffer's dir, won't it? – Kent Oct 10 '13 at 14:02
  • 2
    vim-rooter now runs on all buffers, regardless of file extension, by default. – Andy Stewart Nov 28 '14 at 15:46
  • If someone is interested in a convenient manual fashion and uses already `vim-fugitive`, he can type `:Gcd` to switch to the root directory of the git repository. – Hotschke Oct 11 '17 at 13:56

1 Answers1

3

You could use the vim-rooter plugin with an autocmd to trigger on all files, something like autocmd BufEnter * :Rooter. That's probably the simplest way.

abjuk
  • 3,662
  • 1
  • 13
  • 7
  • Oh that works, I had tried the `autocmd rooter BufEnter * :Rooter` recommended in the `README` but that didn't work, but dropping the `rooter` works fine. Thanks. – foo Oct 10 '13 at 14:30
  • 1
    `rooter` refered to an example `rooter` filetype script created one step before. – romainl Oct 10 '13 at 15:14