how to check if NERDTree is open in vimscript?
Asked Answered
H

2

9

I tried to write a command to save session compatible with NERDTree, I need to check if the NERDTree is open, I can't find any info though Google.

Heavyhearted answered 9/1, 2017 at 5:40 Comment(1)
Please show us what you have tried and what didn't work.Sadoc
Z
8

NERDTree internally stores the name of its scratch buffer (that represents the tree contents in Vim) in a tab-local variable. With bufwinnr(), you can ask Vim whether the buffer is currently loaded in a window.

I use the following function to check for the NERDTree existence (in the current tab page; if you need this globally, you'd have to iterate over all tabs with gettabvar()).

function! IsNerdTreeEnabled()
    return exists('t:NERDTreeBufName') && bufwinnr(t:NERDTreeBufName) != -1
endfunction
Zelmazelten answered 9/1, 2017 at 9:26 Comment(0)
I
12

The NERDTree plugin itself already has a function for this purpose.

g:NERDTree.IsOpen()

Example:

if exists("g:NERDTree") && g:NERDTree.IsOpen()
    ....
endif
Illinois answered 21/9, 2018 at 22:0 Comment(0)
Z
8

NERDTree internally stores the name of its scratch buffer (that represents the tree contents in Vim) in a tab-local variable. With bufwinnr(), you can ask Vim whether the buffer is currently loaded in a window.

I use the following function to check for the NERDTree existence (in the current tab page; if you need this globally, you'd have to iterate over all tabs with gettabvar()).

function! IsNerdTreeEnabled()
    return exists('t:NERDTreeBufName') && bufwinnr(t:NERDTreeBufName) != -1
endfunction
Zelmazelten answered 9/1, 2017 at 9:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.