Skip to content

Commit 81cc9c3

Browse files
cdmillstevearc
andauthored
feat: option to quite vim if oil is closed as last buffer (#491)
* feat: auto-quit vim if oil is closed as last buffer * rename auto_close_vim to auto_close_last_buffer * rework actions.close to be more like actions.cd * fix: configure close action correctly * add type annotation, future proofing * fix: typo * fix: typo * refactor: better type annotations and backwards compatibility --------- Co-authored-by: Steven Arcangeli <[email protected]>
1 parent 21705a1 commit 81cc9c3

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

lua/oil/actions.lua

+10-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,16 @@ M.parent = {
143143

144144
M.close = {
145145
desc = "Close oil and restore original buffer",
146-
callback = oil.close,
146+
callback = function(opts)
147+
opts = opts or {}
148+
oil.close(opts)
149+
end,
150+
parameters = {
151+
exit_if_last_buf = {
152+
type = "boolean",
153+
desc = "Exit vim if oil is closed as the last buffer",
154+
},
155+
},
147156
}
148157

149158
---@param cmd string

lua/oil/init.lua

+13-3
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,13 @@ M.open = function(dir)
379379
update_preview_window()
380380
end
381381

382+
---@class oil.CloseOpts
383+
---@field exit_if_last_buf? boolean Exit vim if this oil buffer is the last open buffer
384+
382385
---Restore the buffer that was present when oil was opened
383-
M.close = function()
386+
---@param opts? oil.CloseOpts
387+
M.close = function(opts)
388+
opts = opts or {}
384389
-- If we're in a floating oil window, close it and try to restore focus to the original window
385390
if vim.w.is_oil_win then
386391
local original_winid = vim.w.oil_original_win
@@ -403,9 +408,14 @@ M.close = function()
403408
-- buffer first
404409
local oilbuf = vim.api.nvim_get_current_buf()
405410
ok = pcall(vim.cmd.bprev)
411+
-- If `bprev` failed, there are no buffers open
406412
if not ok then
407-
-- If `bprev` failed, there are no buffers open so we should create a new one with enew
408-
vim.cmd.enew()
413+
-- either exit or create a new blank buffer
414+
if opts.exit_if_last_buf then
415+
vim.cmd.quit()
416+
else
417+
vim.cmd.enew()
418+
end
409419
end
410420
vim.api.nvim_buf_delete(oilbuf, { force = true })
411421
end

0 commit comments

Comments
 (0)