oil.nvim icon indicating copy to clipboard operation
oil.nvim copied to clipboard

feature request: opening behaviour customization

Open onexbash opened this issue 1 year ago • 4 comments

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

onexbash avatar Jul 10 '24 10:07 onexbash

Or: oil starts with float window rather than full window width buffer

flczcy avatar Jul 12 '24 08:07 flczcy

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,
})

akozlev avatar Aug 12 '24 18:08 akozlev

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!

onexbash avatar Aug 17 '24 22:08 onexbash

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" }),

onexbash avatar Oct 30 '24 15:10 onexbash