minor improvement to ':q' behaviour
prevents odd and slightly annoying situation when ':q' is used and the file tree was open. if quitting the last non-file-tree window, you then get left with a full-screen file tree instead of quitting neovim. this fixes that.
This commit is contained in:
parent
a06e2d805c
commit
c4b95a906d
|
@ -54,3 +54,44 @@ vim.api.nvim_create_autocmd('BufEnter', {
|
|||
end
|
||||
end,
|
||||
})
|
||||
|
||||
-- Borrowed from nvim-tree wiki recipe:
|
||||
-- https://github.com/nvim-tree/nvim-tree.lua/wiki/Recipes#make-q-and-bd-work-as-if-tree-was-not-visible
|
||||
-- Make :bd and :q behave as usual when tree is visible
|
||||
vim.api.nvim_create_autocmd({ 'BufEnter', 'QuitPre' }, {
|
||||
nested = false,
|
||||
group = vim.api.nvim_create_augroup('gered-bufenter-quitpre-nvim-tree', { clear = true }),
|
||||
callback = function(e)
|
||||
local tree = require('nvim-tree.api').tree
|
||||
|
||||
-- Nothing to do if tree is not opened
|
||||
if not tree.is_visible() then
|
||||
return
|
||||
end
|
||||
|
||||
-- How many focusable windows do we have? (excluding e.g. incline status window)
|
||||
local winCount = 0
|
||||
for _, winId in ipairs(vim.api.nvim_list_wins()) do
|
||||
if vim.api.nvim_win_get_config(winId).focusable then
|
||||
winCount = winCount + 1
|
||||
end
|
||||
end
|
||||
|
||||
-- We want to quit and only one window besides tree is left
|
||||
if e.event == 'QuitPre' and winCount == 2 then
|
||||
vim.api.nvim_cmd({ cmd = 'qall' }, {})
|
||||
end
|
||||
|
||||
-- :bd was probably issued an only tree window is left
|
||||
-- Behave as if tree was closed (see `:h :bd`)
|
||||
if e.event == 'BufEnter' and winCount == 1 then
|
||||
-- Required to avoid "Vim:E444: Cannot close last window"
|
||||
vim.defer_fn(function()
|
||||
-- close nvim-tree: will go to the last buffer used before closing
|
||||
tree.toggle { find_file = true, focus = true }
|
||||
-- re-open nivm-tree
|
||||
tree.toggle { find_file = true, focus = false }
|
||||
end, 10)
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue