preservim / nerdtree

A tree explorer plugin for vim.
Do What The F*ck You Want To Public License
19.56k stars 1.44k forks source link

Quit when where is no active buffer #21

Closed michaelkebe closed 12 years ago

michaelkebe commented 14 years ago

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 commented 13 years ago

thanks! this is exactly what I was looking for!

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

blueyed commented 13 years ago

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 commented 12 years ago

It's perfect thanks you :)

scrooloose commented 12 years ago

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 commented 12 years ago

excellent

benmezger commented 12 years ago

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 commented 10 years ago

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 commented 9 years ago

Thanks! This really helps me.

sleepreading commented 9 years ago

thanks,it works!

andfinally commented 8 years ago

O joy! scrooloose's one liner works!

justonia commented 8 years ago

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 commented 8 years ago

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

alexsunxl commented 8 years ago

@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 commented 8 years ago

@justonia thank you!

djsmith42 commented 7 years ago

Thank you! Worked great!

neontorrent commented 7 years ago

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

eromoe commented 7 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

@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 commented 6 years ago

@texasbruce, thank you! Here is my vimrc:

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

jcconnell commented 6 years ago

Still can't figure this out. Any ideas?

neontorrent commented 6 years ago

@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 commented 6 years ago

@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 commented 6 years ago

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

jcconnell commented 6 years ago

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

finviman commented 6 years ago

@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 commented 5 years ago

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

dvdesolve commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

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

PhilRunninger commented 5 years ago

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 commented 5 years ago

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

seamusdemora commented 8 months ago

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 commented 8 months ago

@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 commented 8 months ago

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

SleepyToDeath commented 8 months ago

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 commented 8 months ago

@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 commented 8 months ago

@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 commented 8 months ago

@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 commented 6 months ago

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 commented 6 months ago

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 commented 6 months ago

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!