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

Quit when where is no active buffer #21

Closed
michaelkebe opened this issue Mar 15, 2010 · 45 comments
Closed

Quit when where is no active buffer #21

michaelkebe opened this issue Mar 15, 2010 · 45 comments

Comments

@michaelkebe
Copy link

It would be great if there is no need to quit the NERD_tree* buffers to quit Vim.
Maybe this can be put into the NERDTree as an option?

I have created a small function to get this feature. Just put this code into your .vimrc:

function! NERDTreeQuit()
  redir => buffersoutput
  silent buffers
  redir END
"                     1BufNo  2Mods.     3File           4LineNo
  let pattern = '^\s*\(\d\+\)\(.....\) "\(.*\)"\s\+line \(\d\+\)$'
  let windowfound = 0

  for bline in split(buffersoutput, "\n")
    let m = matchlist(bline, pattern)

    if (len(m) > 0)
      if (m[2] =~ '..a..')
        let windowfound = 1
      endif
    endif
  endfor

  if (!windowfound)
    quitall
  endif
endfunction
autocmd WinEnter * call NERDTreeQuit()

Greetings
Michael

@DeMarko
Copy link

DeMarko commented Nov 14, 2010

thanks! this is exactly what I was looking for!

(if you have a StackOverflow account, you may want to link this here)

@blueyed
Copy link

blueyed commented Mar 23, 2011

A better version appears to be the one used in the janus repo:

autocmd WinEnter * call s:CloseIfOnlyNerdTreeLeft()

" Close all open buffers on entering a window if the only
" buffer that's left is the NERDTree buffer
function! s:CloseIfOnlyNerdTreeLeft()
  if exists("t:NERDTreeBufName")
    if bufwinnr(t:NERDTreeBufName) != -1
      if winnr("$") == 1
        q
      endif
    endif
  endif
endfunction

@geaif
Copy link

geaif commented Dec 22, 2011

It's perfect thanks you :)

@scrooloose
Copy link
Collaborator

Hey guys

This is currently not something that im keen to put in the core plugin, but you can stick something like this in your vimrc to achieve the same thing:

autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif

@qzi
Copy link

qzi commented Apr 3, 2012

excellent

@benmezger
Copy link

Thanks! Been looking for this for some time, got it from here; http://stackoverflow.com/questions/2066590/automatically-quit-vim-if-nerdtree-is-last-and-only-buffer

@jcottrell
Copy link

The solutions above don't seem to account for buffers that are hidden. They close out of vim with hidden buffers still present. Is there something I'm missing?

@kyawzinwin
Copy link

Thanks! This really helps me.

@sleepreading
Copy link

thanks,it works!

@andfinally
Copy link

O joy! scrooloose's one liner works!

@justonia
Copy link

For anyone coming here from Stack Overflow, the one liner above no longer works. The README now says to use this:

autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif

@koem
Copy link

koem commented Jan 26, 2016

michaelkebe's is also not working - It also closes vim when I try to switch to the nerdtree window with Ctrl-w p

gkarthik added a commit to gkarthik/vim-config that referenced this issue Feb 18, 2016
@alexsunxl
Copy link

@justonia thanks !! it is wonderful . I am the guy come from stackoverflow.
By the way , I just want to ask why this old code no work ? cause I upgrade the vim ?
My current vim version is 7.4 ; os is MAC OSX

//old code   no working
autocmd bufenter * if (winnr("$") == 1 && exists('b:NERDTreeType') &&b:NERDTreeType == 'primary') | q | endif

Any how. Thanks you very much!

@lipniagov
Copy link

@justonia thank you!

@djsmith42
Copy link

Thank you! Worked great!

@neontorrent
Copy link

Checking on NERDTreeType no longer works after an update. Now only @justonia 's answer works.

@eromoe
Copy link

eromoe commented Jun 27, 2017

I tried @justonia 's answer, and find it normally work if default open with tree view.
If default open without tree view, and type :NERDTreeCWD or :NERDTreeToggle or ctrl+n, then still need :q twice.

Any idea?

@ghost
Copy link

ghost commented Oct 26, 2017

Hi.
It seems the script in github page no longer work.
I have tried for times but it doesn't work for me when I want to quit vim with nerdtree there.
Exactly this script:
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
My vim version is 8.0 and I have installed NERDTree from Fedora repo.
Any way, How can I solve this problem guys?

@lifecrisis
Copy link
Contributor

lifecrisis commented Oct 27, 2017

I'm afraid I can't replicate your problem. The above snippet does exactly what it claims to do in my Vim installation.

What other customizations are you using? In what script are you running this snippet?

@ghost
Copy link

ghost commented Oct 28, 2017

Sorry guys. I tried several times again with other scripts and finally it seems my problem solved with this script:
`autocmd WinEnter * call s:CloseIfOnlyNerdTreeLeft()

" Close all open buffers on entering a window if the only
" buffer that's left is the NERDTree buffer
function! s:CloseIfOnlyNerdTreeLeft()
if exists("t:NERDTreeBufName")
if bufwinnr(t:NERDTreeBufName) != -1
if winnr("$") == 1
q
endif
endif
endif
endfunction
But thanks anyway. ;)

@jcconnell
Copy link

I am also having an issue with the following script:

autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif

Running Vim 8, ZSH, and iterm on OSX. :q will freeze the window. If I instead :w and then :qall, vim closes everything as expected. It only freezes when I have opened vim with a specific file.

@neontorrent
Copy link

neontorrent commented Nov 14, 2017

@jcconnell Suspect that you might have some config/plugin that opens buffers in the background (hidden). Maybe posting the whole vimrc file will help to diagnose it

@jcconnell
Copy link

@texasbruce, thank you! Here is my vimrc:

https://github.com/jcconnell/config/blob/master/.vimrc

@jcconnell
Copy link

Still can't figure this out. Any ideas?

@neontorrent
Copy link

neontorrent commented Nov 21, 2017

@jcconnell Try adding these in the front of the whole .vimrc file:

set nocompatible              " be iMproved, required
filetype off                  " required

and settings for NERDtree:

autocmd vimenter * NERDTree | wincmd l
autocmd bufenter * if (!exists("t:NERDTreeBufName") ) | silent NERDTreeMirror | wincmd l | endif
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif

@jcconnell
Copy link

@texasbruce thank you!

I tried these settings but I'm still experiencing the same problem. I was able to make a little progress but removing the ! from the following line:
autocmd bufenter * if (!exists("t:NERDTreeBufName") ) | silent NERDTreeMirror | wincmd l | endif

The currently open file buffer closes, but the NERDTree buffer remains open and the whole pane freezes.

@neontorrent
Copy link

@jcconnell Have you tried to disable other plugins and see if this works?

@jcconnell
Copy link

Yes, I've tried disabling all plugins one by one and I'm still having the same problem.

jerrettl added a commit to jerrettl/dotfiles that referenced this issue Jan 1, 2018
@finviman
Copy link

finviman commented Feb 8, 2018

@justonia your answer is work for me. but i use deoplete in neovim, now when i quit vim with a nerdtree opened, deoplete will launch 4 python progress and take 100% cpu.

@NuLL3rr0r
Copy link

@alexsunxl thank you so much! your solution works on neovim.

@dvdesolve
Copy link

Variant with autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif doesn't work with Arch Linux 64-bit, VIM 8.1. However, older solution autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif works fine.

Any ideas?

@PhilRunninger
Copy link
Member

@dvdesolve It looks like you have a very old version of NERDTree installed. The b:NERDTreeType variable isn't used anymore. Can you find out what version you're using? Here are some ways to find out.

  1. In NERDTree, press the ? key. It should display the version number at the top of the buffer.
  2. In the NERDTree source folder, issue these commands. This will show whether you're getting NERDTree from an official source or if it's out of date.
    git rev-parse HEAD
    git remote -v
    
  3. If the last step doesn't work, it may not be a git repository, and may have been installed a different way, a zip file perhaps, from https://www.vim.org/scripts/script.php?script_id=1658

@dvdesolve
Copy link

Method 1 gives 4.2.0. However, Arch Linux package is vim-nerdtree 5.0.0

@PhilRunninger
Copy link
Member

In vim, type :set rtp? to see where NERDTree is installed. You'll have to search through the string to find the NERDTree path portion. Go to that folder and see if step 2 above works. I suspect it won't, and that an ancient NERDTree is bundled with ArchLinux.

@dvdesolve
Copy link

dvdesolve commented Sep 27, 2019

Update to the latest NERDTree solved the problem. Thank you!

@seamusdemora
Copy link

And so... after all of the back & forth, I'm confused. What is it exactly I need in my .vimrc file on a Linux machine running vim ver 8.2?

@rzvxa
Copy link
Member

rzvxa commented Jan 17, 2024

@seamusdemora I believe that the thing you are looking for has been addressed in this section of FAQ.

If you need any more help with your case, feel free to create a new issue for it.

@seamusdemora
Copy link

@rzvxa : Thanks - I missed the FAQ when I looked earlier. Very handy collection!

@SleepyToDeath
Copy link

the autocmd condition in all above scripts better be changed from BufEnter to SafeState, otherwise it won't work on newer versions of vim with mutiple tabs.
See this thread for the discussion: https://groups.google.com/g/vim_dev/c/Cw8McBH6DDM

@rzvxa
Copy link
Member

rzvxa commented Feb 5, 2024

@SleepyToDeath Thanks for bringing it up, I have to admit it is a really good addition; Sadly it isn't still supported in the neovim and it will also break the backward compatibility. It is possible to bind the command to a condition so we use the SafeState if it exists otherwise fallback to the BufEnter but I'm not sure if it is really necessary.

In the NERDTree itself, there are only 2 occurrences of BufEnter, Both of which are in the initial setup of the plugin. One of these 2 is used for hijacking netrw, which I personally am not a fan of since I use both NERDTree and netrw. So it all boils down to only one autocmd listening to the BufEnter event which prevents race conditions that would break the plugin.

As for the FAQ section in the README, There are just there to show some useful tricks for configuring the plugin. While people may use them as it is, It shouldn't be considered as a part of the plugin and just some configuration examples. But it would certainly be nice if we added more detailed answers with more information about both modern Vim features and also examples written in Lua for Neovim users.

Feel free to contribute to any of these 2 areas. I personally think only adding examples with SafeState to FAQ is enough but I wouldn't object if you also change the plugin initialization behavior to get rid of BufEnter in environments where SafeState exists.

@seamusdemora
Copy link

seamusdemora commented Feb 6, 2024

@rzvxa

@seamusdemora I believe that the thing you are looking for has been addressed in this section of FAQ.

If you need any more help with your case, feel free to create a new issue for it.

Sorry to be the pest, but the FAQ doesn't mention where (i.e. which file) to put this... e.g. in ~/.vimrc ??

@rzvxa
Copy link
Member

rzvxa commented Feb 6, 2024

@seamusdemora No worries mate, Yes these are some common configurations and macros for NERDTree, You can copy-paste them into your vimrc to use them. Use them to write your own config the way it suits your needs. You may not need most of them or don't like the way some of them work, So only use the ones that help your case.

@zhitongLIU
Copy link

zhitongLIU commented Mar 17, 2024

hello, i tried to use the Q&A method Or the plugin
but i got this error when nerdtree is the last buffer in a tab

Executing BufEnter Autocommands for "*"
autocommand if tabpagenr('$') == 1 && winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree() | quit | endif

autocommand if winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree() | quit | endif

Error detected while processing BufEnter Autocommands for "*":
 : Not allowed to change the window layout in this autocmd
Executing BufEnter Autocommands for "*"

could somebody help pls?

vim version 9.1.150

@rzvxa
Copy link
Member

rzvxa commented Mar 17, 2024

Hey @zhitongLIU, check the #1411 out. We have to update our FAQ section to reflect Vim9 specifications (in short, you cannot use the quit command in an auto command anymore).

@zhitongLIU
Copy link

Hey @zhitongLIU, check the #1411 out. We have to update our FAQ section to reflect Vim9 specifications (in short, you cannot use the quit command in an auto command anymore).

yes work like a charm, thank you for quick responds!

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

No branches or pull requests