stevearc / oil.nvim

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

feature request: OilToggle / require("oil").toggle() #153

Closed Lamarcke closed 1 year ago

Lamarcke commented 1 year ago

Did you check existing requests?

Describe the feature

It would be helpful to have an OilToggle or require("oil").toggle() API for toggling Oil automatically.

I don't have much experience with Lua, but i'm willing to work on this if someone could point me in a reliable way to know if Oil is open in any buffer.

Provide background

I'm used to having a single key to both opening and closing a file manager/file tree.

Additional details

A good example is :NvimTreeToggle from nvim-tree

EDIT: I'm aware that it's possible to set a custom close keymap in the opts table, but that would mean i had to define and keep the keymaps in sync (both in oil's config and in my keymaps config).

stevearc commented 1 year ago

This is not really a good candidate for inclusion in oil. nvim-tree enforces a convention where the tree opens in a sidebar and you browse it there. By contrast, oil can be open in zero, one, or many windows. There's no enforced convention and more is left up to the end user. The correct way to get this sort of "toggle" behavior is exactly what you've done: set a custom keymap. The only recommendation I would make is to check the filetype rather than string matching the name (it's a bit cleaner)

vim.keymap.set("n", "<leader>e", function()
   if vim.bo.filetype == 'oil' then
     require("oil").close()
   else
     require("oil").open()
   end
end, { desc = "File navigation" })

Alternatively, you could create a keymap to open oil and override it to close oil if you're inside an oil buffer:

vim.keymap.set("n", "<leader>e", "<CMD>Oil<CR>", { desc = "File navigation" })
require("oil").setup({
  keymaps = {
    ["<leader>e"] = "actions.close"
  }
})
Lamarcke commented 1 year ago

I understand, thank you for the clarification.