stevearc / oil.nvim

Neovim file explorer: edit your filesystem like a buffer
MIT License
3.84k stars 110 forks source link

feature request: Add option to open oil in git root #297

Closed timofurrer closed 7 months ago

timofurrer commented 8 months ago

Did you check existing requests?

Describe the feature

The Oil command should support a --in-git-root to set the cwd to the git repository root.

Provide background

I want to be able to easily configure a key mapping that opens oil in the root of the git repository.

What is the significance of this feature?

strongly desired

Additional details

No response

timofurrer commented 8 months ago

I'm currently using lspconfig to find the git ancestor, but it's not as nice.

gregdezeeuw commented 8 months ago

This is what I use to open Oil in the git root of my buffer file.

vim.keymap.set("n", "_g", function()
  local git_path = vim.fn.finddir(".git", ".;")
  local cd_git = vim.fn.fnamemodify(git_path, ":h")
  vim.api.nvim_command(string.format("edit %s", cd_git))
  end, { desc = "edit .git root" })

If I wanted the keymap to also set the cwd to the git root, I would include vim.api.nvim_set_current_dir. So:

vim.keymap.set("n", "_g", function()
  local git_path = vim.fn.finddir(".git", ".;")
  local cd_git = vim.fn.fnamemodify(git_path, ":h")
  vim.api.nvim_command(string.format("edit %s", cd_git))
  vim.api.nvim_set_current_dir(cd_git)
  end, { desc = "edit .git root" })

Not sure if this is along the lines of what you are looking for.

stevearc commented 7 months ago

Thanks @gregdezeeuw! That is the recommended solution for this. I won't be adding a special option for this, so creating your own custom binding for it is the way to go.