stevearc / oil.nvim

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

feature request: Toggle oil buffer without loosing navigation #437

Closed jsongerber closed 3 months ago

jsongerber commented 3 months ago

Did you check existing requests?

Describe the feature

It would be nice to be able to reopen a previously closed oil buffer in the same directory it was. If I use require('oil').open(), it will open either at the cwd, the current buffer directory, or directory that is passed as an argument.
I know that this kind of behavior I'm looking for is maybe more suitable to a tree-view, so please feel free to close this issue if you think it goes against Oil philosophy.

Provide background

Sometimes I use Oil to discover directory structure, and I'm finding myself in a deeply nested level. At that time if I want to quickly close Oil to take a look at an other buffer or do something else, it can be useful to just reopen Oil and continue navigating, instead of starting again from cwd.

What is the significance of this feature?

nice to have

Additional details

I searched issues and source code but I didn't find solution. One solution would be to have a toggle method that does exactly that. Another solution would be to have user cmd that fire at every navigation or action, so it could be possible in my config to keep track of the current directory and use it as a parameter to call open with a specific keymap.

stevearc commented 3 months ago

Try this

local last = nil
vim.api.nvim_create_autocmd("BufEnter", {
  pattern = "oil://*",
  callback = function(args) last = require("oil").get_current_dir() end,
})

vim.keymap.set("n", "<leader>-", function()
  if last then
    require("oil").open(last)
  else
    vim.notify("No previous directory to open", vim.log.levels.WARN)
  end
end)
jsongerber commented 3 months ago

This is perfect and works well thank you! I search for custom autocmd but I didn't think I could use native autocmd. Thanks for your work