(neovim) save-and-exit keymap will try to close just the current buffer

This commit is contained in:
Bartek Stalewski 2025-09-16 01:14:51 +02:00
parent 04feeec6da
commit 40bb137ab1
Signed by: ftpd
SSH key fingerprint: SHA256:vGQiaNJcfUMLt3wF58+Zx15C6SpRDA9spUeggjVY8Yo
2 changed files with 36 additions and 1 deletions

View file

@ -32,7 +32,7 @@ map("n", "[w", diagnostic_goto(false, "WARN"), { desc = "prev warning" })
map("v", "<leader>/", "gc", { desc = "toggle comments", remap = true }) map("v", "<leader>/", "gc", { desc = "toggle comments", remap = true })
map("n", "<esc><cr>", "<cmd>wqa<cr>", { desc = "save and exit", remap = true }) map("n", "<esc><cr>", '<cmd>lua require("utils.quit").close_buffer()<cr>', { desc = "save and exit", remap = true })
map("n", "<tab>", "<C-w>w", { desc = "switch pane", remap = true }) map("n", "<tab>", "<C-w>w", { desc = "switch pane", remap = true })
map("n", "+", ":vertical resize +5<CR>") map("n", "+", ":vertical resize +5<CR>")

View file

@ -0,0 +1,35 @@
local M = {}
count_bufs_by_type = function(loaded_only)
loaded_only = (loaded_only == nil and true or loaded_only)
count = {
normal = 0,
acwrite = 0,
help = 0,
nofile = 0,
nowrite = 0,
quickfix = 0,
terminal = 0,
prompt = 0
}
buftypes = vim.api.nvim_list_bufs()
for _, bufname in pairs(buftypes) do
if (not loaded_only) or vim.api.nvim_buf_is_loaded(bufname) then
buftype = vim.api.nvim_buf_get_option(bufname, 'buftype')
buftype = buftype ~= '' and buftype or 'normal'
count[buftype] = count[buftype] + 1
end
end
return count
end
function M.close_buffer()
local bufTable = count_bufs_by_type()
if (bufTable.normal <= 1) then
local result = vim.api.nvim_exec([[:wq]], true)
else
local result = vim.api.nvim_exec([[:update | bdelete]], true)
end
end
return M