Reputation: 595
If you have any NERDTree instances open when you quit VIM then when you open your session again you get a number of errors:
Error detected while processing BufLeave Autocommands for "NERD_tree_*":
E121: Undefined variable: b:NERDTree
Error detected while processing WinLeave Autocommands for "NERD_tree_*":
Error detected while processing BufLeave Autocommands for "NERD_tree_*":
E121: Undefined variable: b:NERDTree
Error detected while processing WinLeave Autocommands for "NERD_tree_*":
I am trying to execute :tabdo NERDTreeClose
automatically when vim exits (e.g. with qa or wqa or qa! etc)
I am adding:
autocmd VimLeave * tabdo NERDTreeClose<CR>
or
autocmd VimLeave * :tabdo NERDTreeClose<CR>
with or without <CR>
at the end but I can't make it work.
Any suggestions?
Upvotes: 0
Views: 931
Reputation: 50
I have previously encountered the exact same issue with the combination of
creating (and reentering) [neo]Vim
-sessions and multiple NERDTree
-instances.
My solution, which might not be the precise answer that you are looking for
due to the reason that will be explained later, was to first close all NERDTree
instances before creating or updating the [neo]Vim
-Session for later usage.
What you have been trying so far with the tabdo
is thus a few "stages" too
late, as the triggering event for auto-command is VimLeave
.
My primary suggestion (also my personal resort) is to create a quick key-bind that performs the two procedures I stated above combined with a quick save-all in serial:
cnoremap W tabdo NERDTreeClose <bar> wa <bar> mksession! ~/.vim/My_Session.vim
which can be comfortably remembered as the bigger brother of the default w
in
commandline-mode for saving (a single buffer).
The one caveat of this method is that all instances of NERDTree
will be closed
when reentering the session saved from above. This, however, can be somewhat
remedied by calling NERDTreeToggle
with VimEnter
as the triggering event for
autocmd
.
If you insist on utilising autocmd
for this, I would suggest putting the two
procedures enlisted above into a function and use autocmd
to call that
function (with the [neo]Vim
command: call) upon VimLeave
just like you have
tried before, of course under the assumption that you feel compelled to save the session during every exit. I personally do not endorse this, as instances were abundant where
I would quickly fire up [neo]Vim
for a quick note of something inconsequential
and swiftly exit, which certainly does not require that the session be saved.
As a sidenote: the syntax of autocmd:
:au[tocmd] [group] {event} {pat} [++once] [++nested] {cmd}
thus, substitute cmd
with a command directly, i.e., the prefixing colon as you
have tried in your original post is a malpractice.
Upvotes: 3