Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: auto-quit vim if oil is closed as last buffer #491

Merged
merged 8 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ require("oil").setup({
["<C-h>"] = { "actions.select", opts = { horizontal = true }, desc = "Open the entry in a horizontal split" },
["<C-t>"] = { "actions.select", opts = { tab = true }, desc = "Open the entry in new tab" },
["<C-p>"] = "actions.preview",
["<C-c>"] = "actions.close",
["<C-c>"] = { "actions.close", opts = { exit_if_last_buf = false }, desc = "Close oil, optionally exit vim if oil is closed as last buffer" },
["<C-l>"] = "actions.refresh",
["-"] = "actions.parent",
["_"] = "actions.open_cwd",
Expand Down
11 changes: 10 additions & 1 deletion lua/oil/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,16 @@ M.parent = {

M.close = {
desc = "Close oil and restore original buffer",
callback = oil.close,
callback = function(opts)
opts = opts or {}
oil.close(opts.exit_if_last_buf)
end,
parameters = {
exit_if_last_buf = {
type = "boolean",
desc = "Exit vim if oil is closed as the last buffer",
},
},
}

---@param cmd string
Expand Down
2 changes: 1 addition & 1 deletion lua/oil/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ local default_config = {
["<C-h>"] = { "actions.select", opts = { horizontal = true }, desc = "Open the entry in a horizontal split" },
["<C-t>"] = { "actions.select", opts = { tab = true }, desc = "Open the entry in new tab" },
["<C-p>"] = "actions.preview",
["<C-c>"] = "actions.close",
["<C-c>"] = { "actions.close", opts = { exit_if_last_buf = false }, desc = "Close oil, optionally exit vim if oil is closed as last buffer" },
cdmill marked this conversation as resolved.
Show resolved Hide resolved
["<C-l>"] = "actions.refresh",
["-"] = "actions.parent",
["_"] = "actions.open_cwd",
Expand Down
11 changes: 8 additions & 3 deletions lua/oil/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ M.open = function(dir)
end

---Restore the buffer that was present when oil was opened
M.close = function()
M.close = function(exit_if_last_buf)
cdmill marked this conversation as resolved.
Show resolved Hide resolved
-- If we're in a floating oil window, close it and try to restore focus to the original window
if vim.w.is_oil_win then
local original_winid = vim.w.oil_original_win
Expand All @@ -417,8 +417,13 @@ M.close = function()
local oilbuf = vim.api.nvim_get_current_buf()
ok = pcall(vim.cmd.bprev)
if not ok then
-- If `bprev` failed, there are no buffers open so we should create a new one with enew
vim.cmd.enew()
-- If `bprev` failed, there are no buffers open. then,
if exit_if_last_buf then
vim.cmd("quit")
else
-- we should create a new one with enew
vim.cmd.enew()
end
end
vim.api.nvim_buf_delete(oilbuf, { force = true })
end
Expand Down