feature request: opening behaviour customization
Did you check existing requests?
- [X] I have searched the existing issues
Describe the feature
It would be cool to allow the user to define options how Oil should open. For example: oil starts with the preview turned on Or: oil starts in a split on the right side of the screen and the previously opened buffer stays in the split on the left
Provide background
No response
What is the significance of this feature?
nice to have
Additional details
No response
Or: oil starts with float window rather than full window width buffer
You can achieve these behaviours using the neovim API:
-- Open oil in split
vim.keymap.set("n", "<leader>os", "<cmd>vs | Oil<cr>", { desc = "Open oil in split" })
-- Open oil in float window
vim.keymap.set("n", "-", "<cmd>Oil --float<cr>", { desc = "Open parent directory" })
-- Open oil with preview
vim.keymap.set("n", "<leader>op", function()
local oil = require("oil")
local util = require("oil.util")
oil.open()
util.run_after_load(0, function()
oil.select({ preview = true })
end)
end, { desc = "Open oil with preview" })
-- Map q or <escape> to close buffer only for floating Oil buffers
vim.api.nvim_create_autocmd("User", {
group = vim.api.nvim_create_augroup("OilFloatCustom", {}),
pattern = "OilEnter",
callback = function()
local actions = require("oil.actions")
local util = require("oil.util")
if util.is_floating_win() then
vim.keymap.set("n", "<Esc>", actions.close.callback, {
buffer = true,
})
vim.keymap.set("n", "q", actions.close.callback, {
buffer = true,
})
end
end,
})
You can achieve these behaviours using the neovim API:
-- Open oil in split vim.keymap.set("n", "<leader>os", "<cmd>vs | Oil<cr>", { desc = "Open oil in split" })-- Open oil in float window vim.keymap.set("n", "-", "<cmd>Oil --float<cr>", { desc = "Open parent directory" })-- Open oil with preview vim.keymap.set("n", "<leader>op", function() local oil = require("oil") local util = require("oil.util") oil.open() util.run_after_load(0, function() oil.select({ preview = true }) end) end, { desc = "Open oil with preview" })-- Map q or <escape> to close buffer only for floating Oil buffers vim.api.nvim_create_autocmd("User", { group = vim.api.nvim_create_augroup("OilFloatCustom", {}), pattern = "OilEnter", callback = function() local actions = require("oil.actions") local util = require("oil.util") if util.is_floating_win() then vim.keymap.set("n", "<Esc>", actions.close.callback, { buffer = true, }) vim.keymap.set("n", "q", actions.close.callback, { buffer = true, }) end end, })
Thank you mayne!
UPDATE:
oil.select({ preview = true }) is deprecated and oil.open_preview() should be used instead.
This is the updated keymap to open the oil filetree with preview enabled:
vim.keymap.set("n", "<leader>ee", function()
local oil = require("oil")
local util = require("oil.util")
oil.open()
util.run_after_load(0, function()
oil.open_preview()
end)
end, { desc = "oil: open file explorer with preview" }),