-
Notifications
You must be signed in to change notification settings - Fork 250
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
FEATURE: preserve window layout when the last non-neotree-sidebar window is closed #1504
Comments
This comment has been minimized.
This comment has been minimized.
I've checked if LazyVim, which uses neo-tree by default. Guess what, it doesn't happen. I don't really know why. https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/plugins/editor.lua#L4-L127 |
Hi @vricop, the reason why neo-tree does not expand to full screen size after deleting a buffer in LazyVim is not the neo-tree configuration, but rather the way LazyVim deletes a buffer.
local M = {}
--- This function is taken directly from [LazyVim's UI utils](https://github.com/LazyVim/LazyVim/blob/a1c3ec4cd43fe61e3b614237a46ac92771191c81/lua/lazyvim/util/ui.lua#L228).
--- Besides some other nice features, this primarily prevents neo-tree from
--- taking up the whole screen after deleting a buffer.
--- (Thank you folke)
---@param buf number?
function M.bufremove(buf)
buf = buf or 0
buf = buf == 0 and vim.api.nvim_get_current_buf() or buf
if vim.bo.modified then
local choice = vim.fn.confirm(("Save changes to %q?"):format(vim.fn.bufname()), "&Yes\n&No\n&Cancel")
if choice == 0 or choice == 3 then -- 0 for <Esc>/<C-c> and 3 for Cancel
return
end
if choice == 1 then -- Yes
vim.cmd.write()
end
end
for _, win in ipairs(vim.fn.win_findbuf(buf)) do
vim.api.nvim_win_call(win, function()
if not vim.api.nvim_win_is_valid(win) or vim.api.nvim_win_get_buf(win) ~= buf then
return
end
-- Try using alternate buffer
local alt = vim.fn.bufnr("#")
if alt ~= buf and vim.fn.buflisted(alt) == 1 then
vim.api.nvim_win_set_buf(win, alt)
return
end
-- Try using previous buffer
local has_previous = pcall(vim.cmd, "bprevious")
if has_previous and buf ~= vim.api.nvim_win_get_buf(win) then
return
end
-- Create new listed buffer
local new_buf = vim.api.nvim_create_buf(true, false)
vim.api.nvim_win_set_buf(win, new_buf)
end)
end
if vim.api.nvim_buf_is_valid(buf) then
pcall(vim.cmd, "bdelete! " .. buf)
end
end
return M
local ui = require("util.ui")
// ...
map("n", "<leader>bd", ui.bufremove, { desc = "Delete buffer" }) Works great for me. Plus, i get the fancy "Save changes" dialog for free :) All credits go to @folke – Thank you! Best regards |
I have the same problem. I also find that doing a simple |
Hi @JohnWilliston, Best regards |
Well perhaps I misunderstand. I tend to toggle neo-tree open with a keyboard shortcut (F3 from my old CUA days is a hard habit to break). Here's the procedure I follow to evince what seems to me like a bug:
With two files open like that, I would think deleting the buffer that's open in the right pane would leave neo-tree open on the left and the original file from the command line open on the right. What actually happens with I'd happy just change that |
This actually sound like a bug. I assumed, you mean that neovim would exit completely after closing the last buffer (and also with neo-tree still open). Also, I think this issue here can be closed now. A pane taking up the whole screen space after closing another one possibly is default neovim behaviour and not a bug. It can be worked around with the |
Perhaps it may be a good idea to make a pr describing this to the readme documentation or add the utility function to the plugin itself allowing the option to toggle the workaround in the config. |
I know, that’s exactly what I did after commenting it was working in LazyVim. I forgot to mention it here. Here’s mine: https://github.com/vricop/.dotfiles/blob/main/nvim/lua/utils/ui.lua |
This comment has been minimized.
This comment has been minimized.
Good point. |
in my local ui = require("util.ui")
vim.keymap.set("n", "<leader>bd", ui.bufremove, { desc = "Delete Buffer (without breaking neotree)" }) using And since I changed the Also, @vricop creates |
I also have this issue when closing a window.
I can't pretend to understand that Lua code, is there some equivalent to this when closing a window? |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
It's definitely not a bug (at least on neo-tree's side), i agree. however i'll keep this open for now because it seems desirable for neo-tree to have (or document) an opt-in configurable workaround where, whenever you have a neo-tree sidebar open (i.e.
I think adding a "remove buffer but preserve window" command to the plugin itself would be a bit bloated. mini.bufremove and the plugins/commands listed in this vim fandom entry are somewhat popular. |
This comment has been minimized.
This comment has been minimized.
True, also folke's bufdelete is doing that. |
Did you check docs and existing issues?
Neovim Version (nvim -v)
0.10.0 (latest)
Operating System / Version
Linux 6.6.34-1-lts x86_64 GNU/Linux
Describe the Bug
When dealing with multiple buffers neotree will no longer become split view after closing a buffer. This is really annoying because in large co-debases I cant read code and navigate at the same time when this happens. I often just reopen neovim to fix it.
Screenshots, Traceback
expected behaviour


behaviour after closing one buffer out of multiple open (toggling neotree takes up entire screen)
Steps to Reproduce
Expected Behavior
Neotree should always stay in split windows unless directed to do otherwise by the user in the config.
(the minimal config already provided reproduces my issue)
Your Configuration
The text was updated successfully, but these errors were encountered: