airblade / vim-rooter

Changes Vim working directory to project root.
MIT License
1.21k stars 72 forks source link

Add rooter_change_directory_for_project_files option #117

Open barankarakus opened 3 years ago

barankarakus commented 3 years ago

Purpose: To replicate autochdir while still computing root directories and storing them as b:rootDir variables.

airblade commented 3 years ago

In that case wouldn't it be simpler to add two lines to your vimrc instead of using this plugin?

set autochdir
autocmd BufEnter * let b:rootDir = expand('<afile>:p:h')
barankarakus commented 3 years ago

Thanks for getting back to me.

I don't want to set b:rootDir to the directory of the buffer/file being edited:

What's my use case? It's the following:

There is a seemingly more obvious way to achieve this:

  1. Switch vim-rooter to manual mode, so it doesn't take effect at all.
  2. set autochdir
  3. Use the FindRootDirectory() function provided by vim-rooter to access the root directory, rather than b:rootDir

I've found that the problem with this lies in Step 2. autochdir behaves strangely. For example, I defined a command that would search for files from the project root, but every time I invoke that command, autochdir causes me to cd into the project root. Now, I haven't sat down to debug what part of the command might be causing autochdir to behave this way - I adapted the :Files command from the fzf.vim plug-in - but I do get the impression that autochdir should be avoided whenever possible. For one, the vim docs provide a warning that 'some plugins may not work' if autochdir is set.

airblade commented 3 years ago

I think it's worth persevering with your approach a bit longer. I'm not sure why autochdir would be making your command change directory to the project root but presumably it's solvable.

You could try defining a command which turns off autochdir, runs vim-rooter manually, does your actual file-finding, then turns autochdir back on.