stevearc / oil.nvim

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

feature request: Add a Oil Toggle command #384

Closed RiteshChepuri closed 4 months ago

RiteshChepuri commented 4 months ago

Did you check existing requests?

Describe the feature

It would be nice to have oil toggle so that we could return to last or previous buffer.

Provide background

Previously when I open a file tree or file explorer like nvim-tree it used have a toggle command so that we could return back to the previous buffer. I made a command to close the netrw plugin when I switched to it. So it would be nice If we have OilToggle command to close the oil buffer.

What is the significance of this feature?

strongly desired

Additional details

No response

kekscode commented 4 months ago

Would something like this help?

vim.api.nvim_create_user_command("OilToggle", function()
  local current_buf = vim.api.nvim_get_current_buf()
  local current_filetype = vim.api.nvim_buf_get_option(current_buf, "filetype")

  if current_filetype == "oil" then
    -- We use a command to go to the previous buffer
    vim.cmd("b#")
  else
    -- Open oil if not already in an oil buffer
    vim.cmd("Oil")
  end
end, { nargs = 0 })
RiteshChepuri commented 4 months ago

Yeah I think it might work. but rather than returning to previous buffer, we can delete the buffer so that it wont appear on buffers list. Thank you.

keiviv commented 3 months ago

Here are more concise versions (as getting the filetype does not need two calls).

  1. Direct mapping:
    vim.keymap.set('n', '<Tab>', function()
    vim.cmd((vim.bo.filetype == 'oil') and 'bd' or 'Oil')
    end)
  2. Alternatively, if you do want to create a command (for use in multiple keys etc):
    vim.api.nvim_create_user_command('OilToggle', function()
    vim.cmd((vim.bo.filetype == 'oil') and 'bd' or 'Oil')
    end, { nargs = 0 })
halshar commented 1 month ago
vim.keymap.set('n', '<Tab>', function()
   vim.cmd((vim.bo.filetype == 'oil') and 'bd' or 'Oil')
end)

this works like charm, thank you :)