Skip to content

Don't close nvim if current buffer is deleted #708

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

Merged
merged 2 commits into from
Oct 9, 2021

Conversation

taybart
Copy link
Contributor

@taybart taybart commented Oct 8, 2021

Fixes #703

Let me know if there is any refactor/optimization needed

Explanation by line:

local function clear_buffer(absolute_path)
  local bufs = vim.fn.getbufinfo({bufloaded = 1, buflisted = 1}) -- get loaded and listed bufs
  for _, buf in pairs(bufs) do
    if buf.name == absolute_path then -- this is the buffer we want
      if buf.hidden == 0 and #bufs > 1 then -- buffer visible and not alone
        local winnr = api.nvim_get_current_win() -- store old winnr
        api.nvim_set_current_win(buf.windows[1]) -- switch to win
        vim.cmd(':bn') -- switch to next buffer
        api.nvim_set_current_win(winnr) -- go back to original
      end
      vim.api.nvim_buf_delete(buf.bufnr, {}) -- delete the buffer in question
      return -- done
    end
  end
end

@taybart taybart changed the title Don't close vim if current buffer is deleted Don't close nvim if current buffer is deleted Oct 8, 2021
Copy link
Member

@kyazdani42 kyazdani42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd have used more vim.api functions but it's ok like this. thanks for the contribution !

@kyazdani42 kyazdani42 merged commit dbd1343 into nvim-tree:master Oct 9, 2021
@taybart
Copy link
Contributor Author

taybart commented Oct 10, 2021

@kyazdani42 I can use more vim.api functions, I thought vim.fn.getbufinfo was a pretty good one to use for this. I also see that the last api call has vim which it should probably not. Let me know if you want a refactor

@kyazdani42
Copy link
Member

it works, let keep it that way and don't waste your time for such a small detail :)

Almo7aya pushed a commit to Almo7aya/nvim-tree.lua that referenced this pull request Oct 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Deleting a file that is the active buffer closes neovim
2 participants