Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feature request: opening behaviour customization #444

Open
1 task done
onexbash opened this issue Jul 10, 2024 · 4 comments
Open
1 task done

feature request: opening behaviour customization #444

onexbash opened this issue Jul 10, 2024 · 4 comments
Labels
enhancement New feature or request

Comments

@onexbash
Copy link

Did you check existing requests?

  • 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 onexbash added the enhancement New feature or request label Jul 10, 2024
@flczcy
Copy link

flczcy commented Jul 12, 2024

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

@akozlev
Copy link

akozlev commented Aug 12, 2024

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

@onexbash
Copy link
Author

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
Copy link
Author

onexbash commented Oct 30, 2024

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

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants