Skip to content

How to quit Neovim if Neotree is the last remaining window? #137

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

Closed
mrjones2014 opened this issue Feb 10, 2022 · 12 comments
Closed

How to quit Neovim if Neotree is the last remaining window? #137

mrjones2014 opened this issue Feb 10, 2022 · 12 comments
Labels
enhancement New feature or request

Comments

@mrjones2014
Copy link

Hi, just switched from nvim-tree, really impressed so far.

The one thing I'm not seeing that nvim-tree had built in is an option to quit Neovim if the only remaining buffer is the Neotree buffer?

@mrjones2014
Copy link
Author

I've done it with this but it'd be cleaner to have a built-in option IMO:

autocmd WinEnter * if winnr('$') == 1 && getbufvar(winbufnr(winnr()), "&filetype") == "neo-tree" | q | endif

@mrjones2014
Copy link
Author

actually I guess what I want is to quit if Neo-tree is the last window, not just buffer. the above doesn't do that.

@mrjones2014 mrjones2014 changed the title How to quit Neovim if Neotree is the last remaining buffer? How to quit Neovim if Neotree is the last remaining window? Feb 10, 2022
@cseickel cseickel added the enhancement New feature or request label Feb 10, 2022
@cseickel
Copy link
Contributor

I think this autocmd does what you want:

autocmd WinClosed,WinEnter * if winnr('$') == 1 && &ft == "neo-tree" | q | endif

I'm sure this can be added as an option as well.

@mrjones2014
Copy link
Author

@cseickel hm, that's close. It doesn't work if I open Neo-tree before any files. but if I open files, then open Neo-tree, it works.

@cseickel
Copy link
Contributor

OK, I can see how to do this right. It should be easy so I'll add it soon.

@mrjones2014
Copy link
Author

@cseickel I'm getting the following error when I set the new option:

[neo-tree.nvim] [ERROR 14:21:02] ...packer/start/neo-tree.nvim/lua/neo-tree/events/queue.lua:144: Error in event handler for event vim_win_enter[neo-tree-win-enter]: ...im/site/pack/packer/start/neo-tree.nvim/lua/neo-tree.lua:410: Vim:E444: Cannot close last window

@cseickel
Copy link
Contributor

Sorry about that, it's fixed in main, this issue will close when it is released to v1.x

@cseickel cseickel reopened this Feb 12, 2022
@mrjones2014
Copy link
Author

This is still not working for me. I've set the option in config, but Neo-tree still remains open for me when it's the last window.

@cseickel
Copy link
Contributor

It is working for me right now. One thing that may affect you is that the setting only applies if the last window was opened as a sidebar.

Can you share your config and an example of what steps will recreate the issue?

@mrjones2014
Copy link
Author

My Neo-tree config, installed with packer:

return {
  'nvim-neo-tree/neo-tree.nvim',
  requires = { 'MunifTanjim/nui.nvim' },
  config = function()
    require('neo-tree').setup({
      close_if_last_window = true,
      popup_border_style = 'rounded',
      enable_git_status = true,
      enable_diagnostics = true,
      default_component_configs = {
        indent = {
          indent_size = 2,
          padding = 1, -- extra padding on left hand side
          with_markers = true,
          indent_marker = '',
          last_indent_marker = '',
          highlight = 'NeoTreeIndentMarker',
        },
        icon = {
          folder_closed = '',
          folder_open = '',
          default = '',
        },
      },
      filesystem = {
        follow_current_file = true,
        use_libuv_file_watcher = true,
        filters = {
          show_hidden = true,
          respect_gitignore = true,
        },
        window = {
          position = 'right',
          mappings = {
            f = 'none',
          },
        },
      },
    })
  end,
}

To reproduce:

  1. Open a file, then open Neo-tree (or vice versa, order does not matter)
  2. Close the window with the file buffer
  3. Neo-tree should close, but it sometimes doesn't (I just noticed the issue seems to be intermittent)

@cseickel cseickel reopened this Feb 14, 2022
@cseickel
Copy link
Contributor

I'm just guessing because it always works for me, but I added a filter when counting windows to exclude floating windows, and maybe that will fix the issue for you. Let me know if this helps.

@mrjones2014
Copy link
Author

Works for me on branch main!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants