fradeve
fradeve

Reputation: 294

Automatically quit Vim if NERDTree and TagList are the last and only buffers

Basically, my .vimrc starts TagList and NERDTree when Vim is launched, as splits on the left and on the right of the normal file buffer.

I want to close Vim when, closing the last buffer/tab, TagList and NERDTree splits are the only remained. I'm already using vim-nerdtree-tabs and it works great when NERDTree is the only and last buffer open.

I'm aware that such topic has been discussed here on StackOverflow but I cannot find anything related to both NERDTree and TagList.

Thanks

Upvotes: 2

Views: 2176

Answers (4)

webdavis
webdavis

Reputation: 93

This is nice extendable solution. To validate against other plugins/window types just add them to the regex check.

function! s:CloseAddons()
    for w in range(1, winnr('$'))
        if bufname(winbufnr(w)) !~# '__Tagbar\|NERD_tree_\|coc-explorer'
                \ && getbufvar(winbufnr(w), "&buftype") !=? "quickfix"
            return
        endif
    endfor

    if tabpagenr('$') ==? 1
        execute 'quitall'
    else
        execute 'tabclose'
    endif
endfunction

Upvotes: 0

user7596296
user7596296

Reputation: 31

let Tlist_Exit_OnlyWindow = 1 will close Tag_list window if it's the last window, look at http://vim-taglist.sourceforge.net/manual.html for more infomation about Tlist_Exit_OnlyWindow, I'm not sure if you are looking for this, if not, please delete my answer.

Upvotes: 3

Chander Ramesh
Chander Ramesh

Reputation: 3

Improving on Conner's idea, I've made a functional solution here.

" If only 2 windows left, NERDTree and Tag_List, close vim or current tab
fun! NoExcitingBuffersLeft()
  if winnr("$") == 3
    let w1 = bufname(winbufnr(1))
    let w2 = bufname(winbufnr(2))
    let w3 = bufname(winbufnr(3))
    if (exists(":NERDTree")) && (w1 == "__Tag_List__" || w2 == "__Tag_List__" || w3 == "__Tag_List__")
      if tabpagenr("$") == 1
        exec 'qa'
      else
        exec 'tabclose'
      endif
    endif
  endif
endfun
autocmd BufWinLeave * call NoExcitingBuffersLeft()

Need vim 7.0+ for the BufWinLeave event. Closes the tab if more than one tab is open, otherwise quits vim. This way, the auto-command is tied to when you close the last window that's not NERDTree or Tag_List, rather than upon entering one of the two windows.

Upvotes: 0

Conner
Conner

Reputation: 31040

Something like... (untested)

fun! NoExcitingBuffersLeft()
   if tabpagenr("$") == 1 && winnr("$") == 2
      let window1 = bufname(winbufnr(1))
      let window2 = bufname(winbufnr(2))
      if (window1 == t:NERDTreeBufName || window1 == "__Tag_List__") &&
         (window2 == t:NERDTreeBufName || window2 == "__Tag_List__")
         quit
      endif
   endif
endfun

then tie that function to an autocommand...

au WinEnter * call NoExcitingBuffersLeft()<cr>

I don't use either of those plugins, so you may need to adjust the t:NERDTreeBufName and __Tag_List__.

Upvotes: 1

Related Questions