Skip to content
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

Auto-close not work #1005

Closed
tin-pham opened this issue Feb 19, 2022 · 51 comments
Closed

Auto-close not work #1005

tin-pham opened this issue Feb 19, 2022 · 51 comments
Labels
bug Something isn't working

Comments

@tin-pham
Copy link

OS: PopOS
Nvim Version: v0.7.0-dev
Nvim-tree-version: I just update this plugin, but i don't know how to check version of this
Bug: auto-close not work
Step to reproduce: Close the buffer while the tree open
image

image

@vlstv
Copy link

vlstv commented Feb 20, 2022

doesn't work for me as well

@bryant-the-coder
Copy link

OS: PopOS
Nvim Version: v0.7.0-dev
Nvim-tree-version: I just update this plugin, but i don't know how to check version of this
Bug: auto-close not work
Step to reproduce: Close the buffer while the tree open
image

image

font? it doesn't work for me as well

@kyazdani42
Copy link
Member

cannot reproduce, can you provide your configuration and the exact steps to reproduce ?

@tin-pham
Copy link
Author

Open like this
image
While the cursor in the buffer, press :wq or :q to quit

The tree still live
image

I don't know but other people have the same issue as me, I think that the new update problem, when I update this issue start occur

@kyazdani42
Copy link
Member

cannot reproduce, can you share you complete nvim-tree configuration ?
Also if you are using packer, you can navigate to ~/.local/share/nvim/site/pack/packer/start/nvim-tree.lua and do a git log

@tin-pham
Copy link
Author

tin-pham commented Feb 21, 2022

cannot reproduce, can you share you complete nvim-tree configuration ? Also if you are using packer, you can navigate to ~/.local/share/nvim/site/pack/packer/start/nvim-tree.lua and do a git log

My full config
https://github.com/tin-pham/NeoVimLua

nvim-tree inside lua/nvim-tree-config

@kyazdani42 kyazdani42 added the bug Something isn't working label Feb 21, 2022
@kyazdani42
Copy link
Member

cannot reproduce with your config :/ are you sure you are on latest master ?

@tin-pham
Copy link
Author

cannot reproduce with your config :/ are you sure you are on latest master ?

Yep i have updated to the last master
image

Some hour ago I saw the auto-close fix update, but it still not work

@bryant-the-coder
Copy link

@tin-pham what is your font? Auto close works for me now xD

@tin-pham
Copy link
Author

@tin-pham what is your font? Auto close works for me now xD

I used Operator Mono. I still have no luck 😞

@bryant-the-coder
Copy link

so, i was mistaken xD... it didnt close by itself. Do u hvlave operator mono the latest version? @tin-pham

@tin-pham
Copy link
Author

so, i was mistaken xD... it didnt close by itself. Do u hvlave operator mono the latest version? @tin-pham

I don't know, I installed it 3-4 month ago

@tin-pham
Copy link
Author

For a couple of day, now i have figured it out that NeoVim problem, i reinstall it again and now it worked.

@tin-pham
Copy link
Author

=)), it sooooo weird, after coding for a little while, it can't auto close again

@tin-pham tin-pham reopened this Feb 25, 2022
@bryant-the-coder
Copy link

xD i hv nvim tree just in case

@nobleach
Copy link

For the past few releases, I've had the same problem. When I open a file in a new tab, or split, the tree stays open. It's annoying because i now have tab after tab that say, "Nvim Tree". This seems like a regression.

@kyazdani42
Copy link
Member

@nobleach what you describe has nothing to do with this issue. Try setting open_on_tab to false.

Sorry people but i cannot reproduce this issue, i'll leave it open if someone wants to give a shot at fixing it.

@nobleach
Copy link

@nobleach what you describe has nothing to do with this issue. Try setting open_on_tab to false.

Sorry people but i cannot reproduce this issue, i'll leave it open if someone wants to give a shot at fixing it.

Then I'm very sorry, I'll open another issue as open_on_tab still won't close the tree when I open a file in a new tab.

@clee088
Copy link

clee088 commented Mar 16, 2022

Are you calling, require'nvim-tree'.setup {} twice by any chance? When I first configured nvim-tree via packer, I included config = function() require'nvim-tree'.setup {} end; however, I also set up an individual config file, with auto_close = true, which called the setup function again. Removed the call to setup in my packer plugins file and got auto_close to work.

@nobleach
Copy link

Nope, I'm only calling it once. I've been too busy to actually sit down and create a new issue. When it gets to be too big of an annoyance I'll start digging into what it can be. Just this past week nvim-tree started throwing errors when I switch to a new tab, switch back and try to close the tree.... so I'll probably need to check into that a bit as well. I'm really trying to avoid reporting issues as i get this is a project that is being worked on in someone's spare time.... and he's not getting paid to do so.

@dharmx
Copy link

dharmx commented Mar 17, 2022

After making changes you should recompile packer plugin specifications :PackerCompile (assuming you're using packer.nvim)
See if this helps:

lol.mp4

My config.

@nobleach
Copy link

I truly appreciate the time you took to put this together! Unfortunately, PackerCompile seems to do nothing. I see you have a notification plugin that alerts you to its success. I'd typically see some sort of success message in my command lines. I see no indication that anything is happening at all. I do notice your config differs quite a bit from mine, so I might look at wiping my nvim-tree config (it's from almost a year ago) and starting from scratch.

@dharmx
Copy link

dharmx commented Mar 17, 2022

Ah.. you should do that then. But, note that if you're lazy loading the plugin then you'd need to set the global variables beforehand.. (before the plugin is loaded) naturally that you mean that you wouldn't be able to override it's behavior if your using the config key and it'll use defaults! The require part doesn't matter.. you can set it anywhere.

-- Putting everything in config won't work.
use {
  "kyazdani42/nvim-tree.lua",
  cmd = { "NvimTreeToggle", "NvimTreeRefresh", "NvimTreeFocus" },
  config = require "nvimtree",
}

This works for me IMO

use {
  "kyazdani42/nvim-tree.lua",
  cmd = { "NvimTreeToggle", "NvimTreeRefresh", "NvimTreeFocus" },
  setup = require("configs.workflow.nvimtree").before,
  config = require("configs.workflow.nvimtree").after,
  wants = "nvim-notify",
}

NOTE: This only applies if you're lazy loading. And I should mention this as well, I'm using NVIM v0.7.0-dev+1244-g356631cba
Also, don't forget to compile. Atleast compile/restart 4 times lmao.

@kyazdani42
Copy link
Member

@nobleach do not worry about opening a new issue if you have a bug or something you cannot manage to fix. Even if it is an opensource project on people free time, we chose whenever we have time for it, it's not like it's a burden 😄
And yes, you should update your config, it has changed quite a lot in the past year.

@lgds01
Copy link

lgds01 commented Mar 18, 2022

i also meet this problem.And I find when I close the buffer first using :wq, I press q to close the tree, and then the origin buffer will reappear, which means i dont close the buffer completely or reopen the buffer? Need help

@lgds01
Copy link

lgds01 commented Mar 18, 2022

When I use the following settings, this question seems to be solved. But another question appears. When I close one of the buffers(yes, I open many buffers, so there is not only the tree in a window), all of the buffers and tree are closed.
Here is my plugins.lua

use {
    'kyazdani42/nvim-tree.lua',
    requires = {
      'kyazdani42/nvim-web-devicons', -- optional, for file icon
    },
    config = function() require'plugin-config.nvim-tree' end
}

Here is my plugin-config/nvim-tree.lua

require'nvim-tree'.setup {
  auto_close = true,
  git = {
      enable = false
  }
}

Here is my directory structure

├── init.lua
├── lua
│   ├── basic.lua
│   ├── keybindings.lua
│   ├── lsp
│   │   ├── lua.lua
│   │   ├── nvim-cmp.lua
│   │   └── setup.lua
│   ├── plugin-config
│   │   ├── bufferline.lua
│   │   ├── nvim-tree.lua
│   │   └── nvim-treesitter.lua
│   └── plugins.lua
└── plugin
    └── packer_compiled.lua

@alex-courtis
Copy link
Member

But another question appears. When I close one of the buffers(yes, I open many buffers, so there is not only the tree in a window), all of the buffers and tree are closed.

Could you please raise a new bug report for this @lgds-01 ? It will get lost in this issue otherwise ;)

@alex-courtis
Copy link
Member

alex-courtis commented Mar 27, 2022

We could put it in the Tips and Reminders section:

  1. You can automatically close the tab/vim when nvim-tree is the last window in the tab. WARNING: other plugins or automation may interfere with this:
autocmd BufEnter * ++nested if winnr('$') == 1 && bufname() == 'NvimTree_' . tabpagenr() | quit | endif
  1. ...

@tomkimsour
Copy link

Where should you put this line ?

@kyazdani42
Copy link
Member

in your configuration, where you want

@tomkimsour
Copy link

in your configuration, where you want

But this is not lua right? So what if I want to put it in a lua file ?

@WingLim
Copy link

WingLim commented May 3, 2022

You can call nvim_create_autocmd to use it like this in lua:

vim.api.nvim_create_autocmd('BufEnter', {
    command = "if winnr('$') == 1 && bufname() == 'NvimTree_' . tabpagenr() | quit | endif",
    nested = true,
})

@beauwilliams
Copy link

This does not work for be but manually running :if winnr('$') == 1 && bufname() == 'NvimTree_' . tabpagenr() | quit | endif does close the window.

Must be something to do with BufEnter. I suspect maybe caused by wilder.nvim?

@beauwilliams
Copy link

beauwilliams commented May 26, 2022

Still does not work for me on my setup. Pretty sure issue with wilder.nvim. Anyways. I propose we update readme as this method is cleaner and will fire less often.

vim.api.nvim_create_autocmd('Filetype', {
    pattern = "NvimTree",
    command = "if winnr('$') == 1 && bufname() == 'NvimTree_' . tabpagenr() | quit | endif",
    nested = true,
})

@alex-courtis
Copy link
Member

alex-courtis commented May 26, 2022

Still does not work for me on my setup. Pretty sure issue with wilder.nvim.

This "solution" is very hacky and is unlikely to work in complex configurations. It relies on events with unpredictable ordering and nesting, which may be suppressed by other plugins/automation.

Unfortunately, events are the only known means of implementing this close functionality. Of course I would be very happy if someone found a proper solution.

I propose we update readme as this method is cleaner and will fire less often.

Does the FileType approach work more reliably?

@Mariownyou
Copy link

why auto-close feature was removed? I don't need tree every time I open the file. How can I disable this?

@alex-courtis
Copy link
Member

alex-courtis commented May 28, 2022

why auto-close feature was removed? I don't need tree every time I open the file. How can I disable this?

g:auto_close would close vim/tab when nvim-tree was the last window.

I think the functionality you want is :help nvim-tree.actions.open_file.quit_on_open

@Mariownyou
Copy link

Thanks, this is really what I was looking for

@YanzhaoW
Copy link

You can call nvim_create_autocmd to use it like this in lua:

vim.api.nvim_create_autocmd('BufEnter', {
    command = "if winnr('$') == 1 && bufname() == 'NvimTree_' . tabpagenr() | quit | endif",
    nested = true,
})

This is really bad. It can cause neovim to break if you accidentally quit without saving. After that you have to shut down the whole terminal to quit neovim.

@kyazdani42
Copy link
Member

@YanzhaoW do not use it if you don't want to.

@alex-courtis
Copy link
Member

This is getting silly. There is no solution here and we will not build one, nor support hacks to do it:

  • remove from readme/tips
  • add a note to help or readme like "Functionality to automatically close the tab/vim when nvim-tree is the last window in the tab is not present and will not be added, due to complexities in vim's event system."

@kyazdani42 ?

@kyazdani42
Copy link
Member

I wouldn't remove it from the readme since people might just ask how to do it again, but maybe adding more information about why we will not help people with this might be useful.

@theopn
Copy link

theopn commented Jun 26, 2022

You can call nvim_create_autocmd to use it like this in lua:

vim.api.nvim_create_autocmd('BufEnter', {
    command = "if winnr('$') == 1 && bufname() == 'NvimTree_' . tabpagenr() | quit | endif",
    nested = true,
})

This is really bad. It can cause neovim to break if you accidentally quit without saving. After that you have to shut down the whole terminal to quit neovim.

It is not elegant, but you certainly do not have to close the terminal to close the Neovim. Just don't repeatedly type :q to produce million error message, use q: to bring up the command history and choose wq or q! to close the buffer.

@alex-courtis
Copy link
Member

I wouldn't remove it from the readme since people might just ask how to do it again, but maybe adding more information about why we will not help people with this might be useful.

Done

@PROxZIMA
Copy link

PROxZIMA commented Jul 13, 2022

Here's a small change to make it work with unsaved files.

Also I'm extremely new to neovim and lua stuff so any suggestions are welcomed.

-- nvim-tree is also there in modified buffers so this function filter it out
local modifiedBufs = function(bufs)
    local t = 0
    for k,v in pairs(bufs) do
        if v.name:match("NvimTree_") == nil then
            t = t + 1
        end
    end
    return t
end

vim.api.nvim_create_autocmd("BufEnter", {
    nested = true,
    callback = function()
        if #vim.api.nvim_list_wins() == 1 and
        vim.api.nvim_buf_get_name(0):match("NvimTree_") ~= nil and
        modifiedBufs(vim.fn.getbufinfo({bufmodified = 1})) == 0 then
            vim.cmd "quit"
        end
    end
})

Almo7aya pushed a commit to Almo7aya/nvim-tree.lua that referenced this issue Oct 11, 2022
also fix emmylua errors in log.lua file
fixes nvim-tree#1005
fixes nvim-tree#871
WilliamHsieh added a commit to WilliamHsieh/dotfiles that referenced this issue Nov 6, 2022
nvim-tree/nvim-tree.lua#1005 (comment)
seems like there not a proper way to properly auto close
@alex-courtis
Copy link
Member

@PROxZIMA I have added a wiki page covering auto close.

I have added the above solution as it does function in a minimal setup. Please update if you have any new discoveries or enhancements!

@carlos-rodrigo
Copy link

The easiest way I found to do this is following the docs and setup the plugin in this way

require("nvim-tree").setup {
  actions = {
    open_file = {
      quit_on_open = true,
    },
  },
}

@ruperlopez
Copy link

In what part of my config should I put the function(s) provided?
I tried in both: nvim-tree.lua and also as a config inside of my packer manager file. Neither worked.

@gegoune
Copy link
Collaborator

gegoune commented Jul 17, 2023

@ruperlopez What functions? This seems like a generic configuration question, not an issue, please ask under Discussions.

@nvim-tree nvim-tree locked as resolved and limited conversation to collaborators Jul 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests